Skip to content

Commit 7e4a2a8

Browse files
author
Junio C Hamano
committed
avoid asking ?alloc() for zero bytes.
Avoid asking for zero bytes when that change simplifies overall logic. Later we would change the wrapper to ask for 1 byte on platforms that return NULL for zero byte request. Signed-off-by: Junio C Hamano <junkio@cox.net>
1 parent 7d6fb37 commit 7e4a2a8

File tree

6 files changed

+39
-19
lines changed

6 files changed

+39
-19
lines changed

diff.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -504,9 +504,9 @@ static void prepare_temp_file(const char *name,
504504
}
505505
if (S_ISLNK(st.st_mode)) {
506506
int ret;
507-
char *buf, buf_[1024];
508-
buf = ((sizeof(buf_) < st.st_size) ?
509-
xmalloc(st.st_size) : buf_);
507+
char buf[PATH_MAX + 1]; /* ought to be SYMLINK_MAX */
508+
if (sizeof(buf) <= st.st_size)
509+
die("symlink too long: %s", name);
510510
ret = readlink(name, buf, st.st_size);
511511
if (ret < 0)
512512
die("readlink(%s)", name);

diffcore-order.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,13 @@ static int compare_pair_order(const void *a_, const void *b_)
105105
void diffcore_order(const char *orderfile)
106106
{
107107
struct diff_queue_struct *q = &diff_queued_diff;
108-
struct pair_order *o = xmalloc(sizeof(*o) * q->nr);
108+
struct pair_order *o;
109109
int i;
110110

111+
if (!q->nr)
112+
return;
113+
114+
o = xmalloc(sizeof(*o) * q->nr);
111115
prepare_order(orderfile);
112116
for (i = 0; i < q->nr; i++) {
113117
o[i].pair = q->queue[i];

diffcore-pathspec.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ void diffcore_pathspec(const char **pathspec)
4848
for (i = 0; pathspec[i]; i++)
4949
;
5050
speccnt = i;
51+
if (!speccnt)
52+
return;
53+
5154
spec = xmalloc(sizeof(*spec) * speccnt);
5255
for (i = 0; pathspec[i]; i++) {
5356
spec[i].spec = pathspec[i];

index-pack.c

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -352,18 +352,24 @@ static int sha1_compare(const void *_a, const void *_b)
352352
static void write_index_file(const char *index_name, unsigned char *sha1)
353353
{
354354
struct sha1file *f;
355-
struct object_entry **sorted_by_sha =
356-
xcalloc(nr_objects, sizeof(struct object_entry *));
357-
struct object_entry **list = sorted_by_sha;
358-
struct object_entry **last = sorted_by_sha + nr_objects;
355+
struct object_entry **sorted_by_sha, **list, **last;
359356
unsigned int array[256];
360357
int i;
361358
SHA_CTX ctx;
362359

363-
for (i = 0; i < nr_objects; ++i)
364-
sorted_by_sha[i] = &objects[i];
365-
qsort(sorted_by_sha, nr_objects, sizeof(sorted_by_sha[0]),
366-
sha1_compare);
360+
if (nr_objects) {
361+
sorted_by_sha =
362+
xcalloc(nr_objects, sizeof(struct object_entry *));
363+
list = sorted_by_sha;
364+
last = sorted_by_sha + nr_objects;
365+
for (i = 0; i < nr_objects; ++i)
366+
sorted_by_sha[i] = &objects[i];
367+
qsort(sorted_by_sha, nr_objects, sizeof(sorted_by_sha[0]),
368+
sha1_compare);
369+
370+
}
371+
else
372+
sorted_by_sha = list = last = NULL;
367373

368374
unlink(index_name);
369375
f = sha1create("%s", index_name);

read-tree.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -294,17 +294,20 @@ static int unpack_trees(merge_fn_t fn)
294294
{
295295
int indpos = 0;
296296
unsigned len = object_list_length(trees);
297-
struct tree_entry_list **posns =
298-
xmalloc(len * sizeof(struct tree_entry_list *));
297+
struct tree_entry_list **posns;
299298
int i;
300299
struct object_list *posn = trees;
301300
merge_size = len;
302-
for (i = 0; i < len; i++) {
303-
posns[i] = ((struct tree *) posn->item)->entries;
304-
posn = posn->next;
301+
302+
if (len) {
303+
posns = xmalloc(len * sizeof(struct tree_entry_list *));
304+
for (i = 0; i < len; i++) {
305+
posns[i] = ((struct tree *) posn->item)->entries;
306+
posn = posn->next;
307+
}
308+
if (unpack_trees_rec(posns, len, "", fn, &indpos))
309+
return -1;
305310
}
306-
if (unpack_trees_rec(posns, len, "", fn, &indpos))
307-
return -1;
308311

309312
if (trivial_merges_only && nontrivial_merge)
310313
die("Merge requires file-level merging");

tree-diff.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,10 @@ void diff_tree_setup_paths(const char **p)
263263

264264
paths = p;
265265
nr_paths = count_paths(paths);
266+
if (nr_paths == 0) {
267+
pathlens = NULL;
268+
return;
269+
}
266270
pathlens = xmalloc(nr_paths * sizeof(int));
267271
for (i=0; i<nr_paths; i++)
268272
pathlens[i] = strlen(paths[i]);

0 commit comments

Comments
 (0)