merge-file: handle empty files gracefully
Earlier, it would error out while trying to read and/or writing them. Now, calling merge-file with empty files is neither interesting nor useful, but it is a bug that needed fixing. Noticed by Clemens Buchacher. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Johannes Schindelin committed
Mar 13, 2008 at 16:19 UTC
381b851c9b010147bb1fa83a013b7c8f60419026
2 files changed
+4
-3
builtin-merge-file.c
+2
-1
@@ -57,7 +57,8 @@ int cmd_merge_file(int argc, const char **argv, const char *prefix)
57
58
if (!f)
59
ret = error("Could not open %s for writing", filename);
60
- else if (fwrite(result.ptr, result.size, 1, f) != 1)
60
+ else if (result.size &&
61
+ fwrite(result.ptr, result.size, 1, f) != 1)
62
ret = error("Could not write to %s", filename);
63
else if (fclose(f))
64
ret = error("Could not close %s", filename);
xdiff-interface.c
+2
-2
@@ -152,8 +152,8 @@ int read_mmfile(mmfile_t *ptr, const char *filename)
152
if ((f = fopen(filename, "rb")) == NULL)
153
return error("Could not open %s", filename);
154
sz = xsize_t(st.st_size);
155
- ptr->ptr = xmalloc(sz);
156
- if (fread(ptr->ptr, sz, 1, f) != 1)
155
+ ptr->ptr = xmalloc(sz ? sz : 1);
156
+ if (sz && fread(ptr->ptr, sz, 1, f) != 1)
157
return error("Could not read %s", filename);
158
fclose(f);
159
ptr->size = sz;