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>maint
							parent
							
								
									1affea4f62
								
							
						
					
					
						commit
						381b851c9b
					
				|  | @ -57,7 +57,8 @@ int cmd_merge_file(int argc, const char **argv, const char *prefix) | ||||||
|  |  | ||||||
| 		if (!f) | 		if (!f) | ||||||
| 			ret = error("Could not open %s for writing", filename); | 			ret = error("Could not open %s for writing", filename); | ||||||
| 		else if (fwrite(result.ptr, result.size, 1, f) != 1) | 		else if (result.size && | ||||||
|  | 			 fwrite(result.ptr, result.size, 1, f) != 1) | ||||||
| 			ret = error("Could not write to %s", filename); | 			ret = error("Could not write to %s", filename); | ||||||
| 		else if (fclose(f)) | 		else if (fclose(f)) | ||||||
| 			ret = error("Could not close %s", filename); | 			ret = error("Could not close %s", filename); | ||||||
|  |  | ||||||
|  | @ -152,8 +152,8 @@ int read_mmfile(mmfile_t *ptr, const char *filename) | ||||||
| 	if ((f = fopen(filename, "rb")) == NULL) | 	if ((f = fopen(filename, "rb")) == NULL) | ||||||
| 		return error("Could not open %s", filename); | 		return error("Could not open %s", filename); | ||||||
| 	sz = xsize_t(st.st_size); | 	sz = xsize_t(st.st_size); | ||||||
| 	ptr->ptr = xmalloc(sz); | 	ptr->ptr = xmalloc(sz ? sz : 1); | ||||||
| 	if (fread(ptr->ptr, sz, 1, f) != 1) | 	if (sz && fread(ptr->ptr, sz, 1, f) != 1) | ||||||
| 		return error("Could not read %s", filename); | 		return error("Could not read %s", filename); | ||||||
| 	fclose(f); | 	fclose(f); | ||||||
| 	ptr->size = sz; | 	ptr->size = sz; | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue
	
	 Johannes Schindelin
						Johannes Schindelin