Skip to contentSkip to conversation

get_shallow_commits: Avoid memory leak if a commit has been reached already.

PATCH In review Alexandre Julliard 4 participants 5 messages Aug 11, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 3/5 get_shallow_commits: Avoid memory leak if a commit has been reached already.

---
 shallow.c |    4 +++-
 1 files changed, 3 insertions(+), 1 deletions(-)
shallow.c +3 -1
index 2db1dc4..3d53d17 100644
@@ -60,7 +60,9 @@ struct commit_list *get_shallow_commits(
 					commit = NULL;
 					continue;
 				}
-				commit->util = xcalloc(1, sizeof(int));
+				if (!commit->util)
+					commit->util = xmalloc(sizeof(int));
+				*(int *)commit->util = 0;
 				cur_depth = 0;
 			} else {
 				commit = (struct commit *)
Signature · 4 lines
1.4.4.1.ga335e

-- 
Alexandre Julliard
#1 lore raw
Quoted 10 lines · Alexandre Julliard
---
 shallow.c |    4 +++-
 1 files changed, 3 insertions(+), 1 deletions(-)
shallow.c +0 -0
index 2db1dc4..3d53d17 100644

shallow.c struct commit_list *get_shallow_commits( · line 66

Quoted 8 lines
shallow.c +3 -1
@@ -60,7 +60,9 @@ struct commit_list *get_shallow_commits(
 					commit = NULL;
 					continue;
 				}
-				commit->util = xcalloc(1, sizeof(int));
+				if (!commit->util)
+					commit->util = xmalloc(sizeof(int));
+				*(int *)commit->util = 0;
 				cur_depth = 0;

Hi,

Any reason you didn't do:

if (!commit->util)

	commit->util = xcalloc(1, sizeof(int));

That would seem to be the same.

Regards,
Anand

#2 lore raw

Hi,

Quoted 4 lines · Anand Kumria

Any reason you didn't do:

if (!commit->util)

	commit->util = xcalloc(1, sizeof(int));

xmalloc() does not initialize the memory. Therefore, it is usually preferred if you initialize the memory yourself. In this case, the memory is initialized to 0, even if it is not allocated.

It may be a minor performance issue, but it is a good habit to use xcalloc only if it is needed.

Ciao,
Dscho

#3 lore raw
Quoted 7 lines · Johannes Schindelin
Quoted 4 lines · Anand Kumria

Any reason you didn't do:

if (!commit->util)

	commit->util = xcalloc(1, sizeof(int));
Quoted 6 lines

xmalloc() does not initialize the memory. Therefore, it is usually preferred if you initialize the memory yourself. In this case, the memory is initialized to 0, even if it is not allocated.

It may be a minor performance issue, but it is a good habit to use xcalloc only if it is needed.

I think you could pretend the commit->util field to be of some kind of int and avoid the allocation altogether ;-).

#4 lore raw

Hi,

Quoted 2 lines · Junio C Hamano

I think you could pretend the commit->util field to be of some kind of int and avoid the allocation altogether ;-).

I actually thought about that, and found it too dangerous. But of course, since we need at least a 32-bit architecture, chances are very high that a pointer is castable to an int.

Ciao,
Dscho