@matheusbsbs / matheus-bagliano / commits / 7e4a2a8483
Fork 0

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>

Junio C Hamano committed Dec 26, 2005 at 12:34 UTC 7e4a2a848377241b8fb4f624d1151bbf2f8d5814
6 files changed +39 -19
diff.c
+3 -3
@@ -504,9 +504,9 @@ static void prepare_temp_file(const char *name,
504 }
505 if (S_ISLNK(st.st_mode)) {
506 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);
510 ret = readlink(name, buf, st.st_size);
511 if (ret < 0)
512 die("readlink(%s)", name);
diffcore-order.c
+5 -1
@@ -105,9 +105,13 @@ static int compare_pair_order(const void *a_, const void *b_)
105 void diffcore_order(const char *orderfile)
106 {
107 struct diff_queue_struct *q = &diff_queued_diff;
108 - struct pair_order *o = xmalloc(sizeof(*o) * q->nr);
108 + struct pair_order *o;
109 int i;
110
111 + if (!q->nr)
112 + return;
113 +
114 + o = xmalloc(sizeof(*o) * q->nr);
115 prepare_order(orderfile);
116 for (i = 0; i < q->nr; i++) {
117 o[i].pair = q->queue[i];
diffcore-pathspec.c
+3
@@ -48,6 +48,9 @@ void diffcore_pathspec(const char **pathspec)
48 for (i = 0; pathspec[i]; i++)
49 ;
50 speccnt = i;
51 + if (!speccnt)
52 + return;
53 +
54 spec = xmalloc(sizeof(*spec) * speccnt);
55 for (i = 0; pathspec[i]; i++) {
56 spec[i].spec = pathspec[i];
index-pack.c
+14 -8
@@ -352,18 +352,24 @@ static int sha1_compare(const void *_a, const void *_b)
352 static void write_index_file(const char *index_name, unsigned char *sha1)
353 {
354 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;
356 unsigned int array[256];
357 int i;
358 SHA_CTX ctx;
359
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;
373
374 unlink(index_name);
375 f = sha1create("%s", index_name);
read-tree.c
+10 -7
@@ -294,17 +294,20 @@ static int unpack_trees(merge_fn_t fn)
294 {
295 int indpos = 0;
296 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;
298 int i;
299 struct object_list *posn = trees;
300 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;
310 }
306 - if (unpack_trees_rec(posns, len, "", fn, &indpos))
307 - return -1;
311
312 if (trivial_merges_only && nontrivial_merge)
313 die("Merge requires file-level merging");
tree-diff.c
+4
@@ -263,6 +263,10 @@ void diff_tree_setup_paths(const char **p)
263
264 paths = p;
265 nr_paths = count_paths(paths);
266 + if (nr_paths == 0) {
267 + pathlens = NULL;
268 + return;
269 + }
270 pathlens = xmalloc(nr_paths * sizeof(int));
271 for (i=0; i<nr_paths; i++)
272 pathlens[i] = strlen(paths[i]);