Skip to content

Commit c50424a

Browse files
committed
Merge branch 'jk/write-in-full-fix'
Many codepaths did not diagnose write failures correctly when disks go full, due to their misuse of write_in_full() helper function, which have been corrected. * jk/write-in-full-fix: read_pack_header: handle signed/unsigned comparison in read result config: flip return value of store_write_*() notes-merge: use ssize_t for write_in_full() return value pkt-line: check write_in_full() errors against "< 0" convert less-trivial versions of "write_in_full() != len" avoid "write_in_full(fd, buf, len) != len" pattern get-tar-commit-id: check write_in_full() return against 0 config: avoid "write_in_full(fd, buf, len) < len" pattern
2 parents 94982b6 + f48ecd3 commit c50424a

22 files changed

+65
-67
lines changed

builtin/get-tar-commit-id.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ int cmd_get_tar_commit_id(int argc, const char **argv, const char *prefix)
3333
if (!skip_prefix(content, "52 comment=", &comment))
3434
return 1;
3535

36-
n = write_in_full(1, comment, 41);
37-
if (n < 41)
36+
if (write_in_full(1, comment, 41) < 0)
3837
die_errno("git get-tar-commit-id: write error");
3938

4039
return 0;

builtin/receive-pack.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,7 @@ static int run_and_feed_hook(const char *hook_name, feed_fn feed,
743743
size_t n;
744744
if (feed(feed_state, &buf, &n))
745745
break;
746-
if (write_in_full(proc.in, buf, n) != n)
746+
if (write_in_full(proc.in, buf, n) < 0)
747747
break;
748748
}
749749
close(proc.in);

builtin/rerere.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ static int outf(void *dummy, mmbuffer_t *ptr, int nbuf)
1818
{
1919
int i;
2020
for (i = 0; i < nbuf; i++)
21-
if (write_in_full(1, ptr[i].ptr, ptr[i].size) != ptr[i].size)
21+
if (write_in_full(1, ptr[i].ptr, ptr[i].size) < 0)
2222
return -1;
2323
return 0;
2424
}

builtin/unpack-file.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ static char *create_temp_file(struct object_id *oid)
1515

1616
xsnprintf(path, sizeof(path), ".merge_file_XXXXXX");
1717
fd = xmkstemp(path);
18-
if (write_in_full(fd, buf, size) != size)
18+
if (write_in_full(fd, buf, size) < 0)
1919
die_errno("unable to write temp-file");
2020
close(fd);
2121
return path;

config.c

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2292,10 +2292,11 @@ static int write_error(const char *filename)
22922292
return 4;
22932293
}
22942294

2295-
static int store_write_section(int fd, const char *key)
2295+
static ssize_t write_section(int fd, const char *key)
22962296
{
22972297
const char *dot;
2298-
int i, success;
2298+
int i;
2299+
ssize_t ret;
22992300
struct strbuf sb = STRBUF_INIT;
23002301

23012302
dot = memchr(key, '.', store.baselen);
@@ -2311,15 +2312,16 @@ static int store_write_section(int fd, const char *key)
23112312
strbuf_addf(&sb, "[%.*s]\n", store.baselen, key);
23122313
}
23132314

2314-
success = write_in_full(fd, sb.buf, sb.len) == sb.len;
2315+
ret = write_in_full(fd, sb.buf, sb.len);
23152316
strbuf_release(&sb);
23162317

2317-
return success;
2318+
return ret;
23182319
}
23192320

2320-
static int store_write_pair(int fd, const char *key, const char *value)
2321+
static ssize_t write_pair(int fd, const char *key, const char *value)
23212322
{
2322-
int i, success;
2323+
int i;
2324+
ssize_t ret;
23232325
int length = strlen(key + store.baselen + 1);
23242326
const char *quote = "";
23252327
struct strbuf sb = STRBUF_INIT;
@@ -2359,10 +2361,10 @@ static int store_write_pair(int fd, const char *key, const char *value)
23592361
}
23602362
strbuf_addf(&sb, "%s\n", quote);
23612363

2362-
success = write_in_full(fd, sb.buf, sb.len) == sb.len;
2364+
ret = write_in_full(fd, sb.buf, sb.len);
23632365
strbuf_release(&sb);
23642366

2365-
return success;
2367+
return ret;
23662368
}
23672369

23682370
static ssize_t find_beginning_of_line(const char *contents, size_t size,
@@ -2491,8 +2493,8 @@ int git_config_set_multivar_in_file_gently(const char *config_filename,
24912493
}
24922494

24932495
store.key = (char *)key;
2494-
if (!store_write_section(fd, key) ||
2495-
!store_write_pair(fd, key, value))
2496+
if (write_section(fd, key) < 0 ||
2497+
write_pair(fd, key, value) < 0)
24962498
goto write_err_out;
24972499
} else {
24982500
struct stat st;
@@ -2602,11 +2604,10 @@ int git_config_set_multivar_in_file_gently(const char *config_filename,
26022604
/* write the first part of the config */
26032605
if (copy_end > copy_begin) {
26042606
if (write_in_full(fd, contents + copy_begin,
2605-
copy_end - copy_begin) <
2606-
copy_end - copy_begin)
2607+
copy_end - copy_begin) < 0)
26072608
goto write_err_out;
26082609
if (new_line &&
2609-
write_str_in_full(fd, "\n") != 1)
2610+
write_str_in_full(fd, "\n") < 0)
26102611
goto write_err_out;
26112612
}
26122613
copy_begin = store.offset[i];
@@ -2615,18 +2616,17 @@ int git_config_set_multivar_in_file_gently(const char *config_filename,
26152616
/* write the pair (value == NULL means unset) */
26162617
if (value != NULL) {
26172618
if (store.state == START) {
2618-
if (!store_write_section(fd, key))
2619+
if (write_section(fd, key) < 0)
26192620
goto write_err_out;
26202621
}
2621-
if (!store_write_pair(fd, key, value))
2622+
if (write_pair(fd, key, value) < 0)
26222623
goto write_err_out;
26232624
}
26242625

26252626
/* write the rest of the config */
26262627
if (copy_begin < contents_sz)
26272628
if (write_in_full(fd, contents + copy_begin,
2628-
contents_sz - copy_begin) <
2629-
contents_sz - copy_begin)
2629+
contents_sz - copy_begin) < 0)
26302630
goto write_err_out;
26312631

26322632
munmap(contents, contents_sz);
@@ -2803,7 +2803,7 @@ int git_config_rename_section_in_file(const char *config_filename,
28032803
continue;
28042804
}
28052805
store.baselen = strlen(new_name);
2806-
if (!store_write_section(out_fd, new_name)) {
2806+
if (write_section(out_fd, new_name) < 0) {
28072807
ret = write_error(get_lock_file_path(lock));
28082808
goto out;
28092809
}
@@ -2829,7 +2829,7 @@ int git_config_rename_section_in_file(const char *config_filename,
28292829
if (remove)
28302830
continue;
28312831
length = strlen(output);
2832-
if (write_in_full(out_fd, output, length) != length) {
2832+
if (write_in_full(out_fd, output, length) < 0) {
28332833
ret = write_error(get_lock_file_path(lock));
28342834
goto out;
28352835
}

diff.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3738,7 +3738,7 @@ static void prep_temp_blob(const char *path, struct diff_tempfile *temp,
37383738
blob = buf.buf;
37393739
size = buf.len;
37403740
}
3741-
if (write_in_full(temp->tempfile->fd, blob, size) != size ||
3741+
if (write_in_full(temp->tempfile->fd, blob, size) < 0 ||
37423742
close_tempfile_gently(temp->tempfile))
37433743
die_errno("unable to write temp-file");
37443744
temp->name = get_tempfile_path(temp->tempfile);

entry.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,8 @@ static int write_entry(struct cache_entry *ce,
257257
char *new;
258258
struct strbuf buf = STRBUF_INIT;
259259
unsigned long size;
260-
size_t wrote, newsize = 0;
260+
ssize_t wrote;
261+
size_t newsize = 0;
261262
struct stat st;
262263
const struct submodule *sub;
263264

@@ -332,7 +333,7 @@ static int write_entry(struct cache_entry *ce,
332333
fstat_done = fstat_output(fd, state, &st);
333334
close(fd);
334335
free(new);
335-
if (wrote != size)
336+
if (wrote < 0)
336337
return error("unable to write file %s", path);
337338
break;
338339
case S_IFGITLINK:

fast-import.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2952,7 +2952,7 @@ static void parse_reset_branch(const char *arg)
29522952

29532953
static void cat_blob_write(const char *buf, unsigned long size)
29542954
{
2955-
if (write_in_full(cat_blob_fd, buf, size) != size)
2955+
if (write_in_full(cat_blob_fd, buf, size) < 0)
29562956
die_errno("Write to frontend failed");
29572957
}
29582958

http-backend.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ static void inflate_request(const char *prog_name, int out, int buffer_input)
358358
die("zlib error inflating request, result %d", ret);
359359

360360
n = stream.total_out - cnt;
361-
if (write_in_full(out, out_buf, n) != n)
361+
if (write_in_full(out, out_buf, n) < 0)
362362
die("%s aborted reading request", prog_name);
363363
cnt += n;
364364

@@ -379,7 +379,7 @@ static void copy_request(const char *prog_name, int out)
379379
ssize_t n = read_request(0, &buf);
380380
if (n < 0)
381381
die_errno("error reading request body");
382-
if (write_in_full(out, buf, n) != n)
382+
if (write_in_full(out, buf, n) < 0)
383383
die("%s aborted reading request", prog_name);
384384
close(out);
385385
free(buf);

ll-merge.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ static void create_temp(mmfile_t *src, char *path, size_t len)
154154

155155
xsnprintf(path, len, ".merge_file_XXXXXX");
156156
fd = xmkstemp(path);
157-
if (write_in_full(fd, src->ptr, src->size) != src->size)
157+
if (write_in_full(fd, src->ptr, src->size) < 0)
158158
die_errno("unable to write temp-file");
159159
close(fd);
160160
}

0 commit comments

Comments
 (0)