[PATCH] alloc_ref(): allow for trailing NUL

Subsystems: the rest

DORMANTno replies

2 messages, 2 authors, 2016-06-15 · open the first message on its own page

[PATCH] alloc_ref(): allow for trailing NUL

From: Johannes Schindelin <hidden>
Date: 2016-06-15 22:43:37

The parameter name "namelen" suggests that you pass the equivalent of
strlen() to the function alloc_ref().  However, this function did not
allocate enough space to put a NUL after the name.

Since struct ref does not have any member to describe the length of the
string, this just does not make sense.

So make space for the NUL.

Signed-off-by: Johannes Schindelin <redacted>
---
 remote.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
diff --git a/remote.c b/remote.c
index e7d735b..7531c92 100644
--- a/remote.c
+++ b/remote.c
@@ -453,7 +453,7 @@ int remote_find_tracking(struct remote *remote, struct refspec *refspec)
 
 struct ref *alloc_ref(unsigned namelen)
 {
-	struct ref *ret = xmalloc(sizeof(struct ref) + namelen);
+	struct ref *ret = xmalloc(sizeof(struct ref) + namelen + 1);
 	memset(ret, 0, sizeof(struct ref) + namelen);
 	return ret;
 }
-- 
1.5.3.2.1102.g9487

Re: [PATCH] alloc_ref(): allow for trailing NUL

From: Daniel Barkalow <hidden>
Date: 2016-06-15 22:43:37

On Fri, 28 Sep 2007, Johannes Schindelin wrote:
The parameter name "namelen" suggests that you pass the equivalent of
strlen() to the function alloc_ref().  However, this function did not
allocate enough space to put a NUL after the name.

Since struct ref does not have any member to describe the length of the
string, this just does not make sense.

So make space for the NUL.
Good point, but shouldn't you then fix call sites that use strlen(name) + 
1?

	-Daniel
*This .sig left intentionally blank*
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help