X-Git-Url: https://asedeno.scripts.mit.edu/gitweb/?a=blobdiff_plain;f=dir.c;h=133c333df61be37e7908f77367f63c85cfc9c548;hb=0901d5a2ef80996cf64c8afeaec765e1bc09f795;hp=1538ad5da30e08c03e4297ceb369b7a09a301af4;hpb=030b1a77f72a7e3307c7d7881ae570ca1c8ed877;p=git.git diff --git a/dir.c b/dir.c index 1538ad5da..133c333df 100644 --- a/dir.c +++ b/dir.c @@ -242,6 +242,14 @@ int add_excludes_from_file_to_list(const char *fname, if (!check_index || (buf = read_skip_worktree_file_from_index(fname, &size)) == NULL) return -1; + if (size == 0) { + free(buf); + return 0; + } + if (buf[size-1] != '\n') { + buf = xrealloc(buf, size+1); + buf[size++] = '\n'; + } } else { size = xsize_t(st.st_size); @@ -249,19 +257,21 @@ int add_excludes_from_file_to_list(const char *fname, close(fd); return 0; } - buf = xmalloc(size); + buf = xmalloc(size+1); if (read_in_full(fd, buf, size) != size) { + free(buf); close(fd); return -1; } + buf[size++] = '\n'; close(fd); } if (buf_p) *buf_p = buf; entry = buf; - for (i = 0; i <= size; i++) { - if (i == size || buf[i] == '\n') { + for (i = 0; i < size; i++) { + if (buf[i] == '\n') { if (entry != buf + i && entry[0] != '#') { buf[i - (i && buf[i-1] == '\r')] = 0; add_exclude(entry, base, baselen, which); @@ -1034,7 +1044,7 @@ int remove_path(const char *name) slash = dirs + (slash - name); do { *slash = '\0'; - } while (rmdir(dirs) && (slash = strrchr(dirs, '/'))); + } while (rmdir(dirs) == 0 && (slash = strrchr(dirs, '/'))); free(dirs); } return 0;