shared: add nm_memdup() as replacement for g_memdup()
I think g_memdup() is dangerous for integer overflow. There is no need for accepting this danger, just use our own nm_memdup() which does not have this flaw.
This commit is contained in:
@@ -190,6 +190,37 @@ nm_ip_addr_set (int addr_family, gpointer dst, const NMIPAddr *src)
|
|||||||
|
|
||||||
/*****************************************************************************/
|
/*****************************************************************************/
|
||||||
|
|
||||||
|
/* like g_memdup(). The difference is that the @size argument is of type
|
||||||
|
* gsize, while g_memdup() has type guint. Since, the size of container types
|
||||||
|
* like GArray is guint as well, this means trying to g_memdup() an
|
||||||
|
* array,
|
||||||
|
* g_memdup (array->data, array->len * sizeof (ElementType))
|
||||||
|
* will lead to integer overflow, if there are more than G_MAXUINT/sizeof(ElementType)
|
||||||
|
* bytes. That seems unnecessarily dangerous to me.
|
||||||
|
* nm_memdup() avoids that, because its size argument is always large enough
|
||||||
|
* to contain all data that a GArray can hold.
|
||||||
|
*
|
||||||
|
* Another minor difference to g_memdup() is that the glib version also
|
||||||
|
* returns %NULL if @data is %NULL. E.g. g_memdup(NULL, 1)
|
||||||
|
* gives %NULL, but nm_memdup(NULL, 1) crashes. I think that
|
||||||
|
* is desirable, because @size MUST be correct at all times. @size
|
||||||
|
* may be zero, but one must not claim to have non-zero bytes when
|
||||||
|
* passing a %NULL @data pointer.
|
||||||
|
*/
|
||||||
|
static inline gpointer
|
||||||
|
nm_memdup (gconstpointer data, gsize size)
|
||||||
|
{
|
||||||
|
gpointer p;
|
||||||
|
|
||||||
|
if (size == 0)
|
||||||
|
return NULL;
|
||||||
|
p = g_malloc (size);
|
||||||
|
memcpy (p, data, size);
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*****************************************************************************/
|
||||||
|
|
||||||
extern const void *const _NM_PTRARRAY_EMPTY[1];
|
extern const void *const _NM_PTRARRAY_EMPTY[1];
|
||||||
|
|
||||||
#define NM_PTRARRAY_EMPTY(type) ((type const*) _NM_PTRARRAY_EMPTY)
|
#define NM_PTRARRAY_EMPTY(type) ((type const*) _NM_PTRARRAY_EMPTY)
|
||||||
|
Reference in New Issue
Block a user