Thread (6 messages) flat view 6 messages, 2 authors, 2016-06-15

Re: [PATCH 1/4] Add xmallocz()

From: Bill Lear <hidden>
Date: 2016-06-15 22:48:06

On Tuesday, January 26, 2010 at 20:24:12 (+0200) Ilari Liusvaara writes:
Add routine for allocating NUL-terminated memory block without risking
integer overflow in addition of +1 for NUL byte.
...
void *xmemdupz(const void *data, size_t len)
{
-	char *p = xmalloc(len + 1);
+	char *p = xmallocz(len);
	memcpy(p, data, len);
	p[len] = '\0';
	return p;
Do you need the statement

 	p[len] = '\0';

any longer in the above?  If not, could you just do this:

void *xmemdupz(const void *data, size_t len)
{
	return memcpy(xmallocz(len), data, len);
}


??


Bill
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help