Skip to content

Commit 96c6bb5

Browse files
committed
Merge branch 'jk/write-in-full-fix' into maint
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 7186408 + f48ecd3 commit 96c6bb5

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
@@ -742,7 +742,7 @@ static int run_and_feed_hook(const char *hook_name, feed_fn feed,
742742
size_t n;
743743
if (feed(feed_state, &buf, &n))
744744
break;
745-
if (write_in_full(proc.in, buf, n) != n)
745+
if (write_in_full(proc.in, buf, n) < 0)
746746
break;
747747
}
748748
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(unsigned char *sha1)
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
@@ -2247,10 +2247,11 @@ static int write_error(const char *filename)
22472247
return 4;
22482248
}
22492249

2250-
static int store_write_section(int fd, const char *key)
2250+
static ssize_t write_section(int fd, const char *key)
22512251
{
22522252
const char *dot;
2253-
int i, success;
2253+
int i;
2254+
ssize_t ret;
22542255
struct strbuf sb = STRBUF_INIT;
22552256

22562257
dot = memchr(key, '.', store.baselen);
@@ -2266,15 +2267,16 @@ static int store_write_section(int fd, const char *key)
22662267
strbuf_addf(&sb, "[%.*s]\n", store.baselen, key);
22672268
}
22682269

2269-
success = write_in_full(fd, sb.buf, sb.len) == sb.len;
2270+
ret = write_in_full(fd, sb.buf, sb.len);
22702271
strbuf_release(&sb);
22712272

2272-
return success;
2273+
return ret;
22732274
}
22742275

2275-
static int store_write_pair(int fd, const char *key, const char *value)
2276+
static ssize_t write_pair(int fd, const char *key, const char *value)
22762277
{
2277-
int i, success;
2278+
int i;
2279+
ssize_t ret;
22782280
int length = strlen(key + store.baselen + 1);
22792281
const char *quote = "";
22802282
struct strbuf sb = STRBUF_INIT;
@@ -2314,10 +2316,10 @@ static int store_write_pair(int fd, const char *key, const char *value)
23142316
}
23152317
strbuf_addf(&sb, "%s\n", quote);
23162318

2317-
success = write_in_full(fd, sb.buf, sb.len) == sb.len;
2319+
ret = write_in_full(fd, sb.buf, sb.len);
23182320
strbuf_release(&sb);
23192321

2320-
return success;
2322+
return ret;
23212323
}
23222324

23232325
static ssize_t find_beginning_of_line(const char *contents, size_t size,
@@ -2446,8 +2448,8 @@ int git_config_set_multivar_in_file_gently(const char *config_filename,
24462448
}
24472449

24482450
store.key = (char *)key;
2449-
if (!store_write_section(fd, key) ||
2450-
!store_write_pair(fd, key, value))
2451+
if (write_section(fd, key) < 0 ||
2452+
write_pair(fd, key, value) < 0)
24512453
goto write_err_out;
24522454
} else {
24532455
struct stat st;
@@ -2557,11 +2559,10 @@ int git_config_set_multivar_in_file_gently(const char *config_filename,
25572559
/* write the first part of the config */
25582560
if (copy_end > copy_begin) {
25592561
if (write_in_full(fd, contents + copy_begin,
2560-
copy_end - copy_begin) <
2561-
copy_end - copy_begin)
2562+
copy_end - copy_begin) < 0)
25622563
goto write_err_out;
25632564
if (new_line &&
2564-
write_str_in_full(fd, "\n") != 1)
2565+
write_str_in_full(fd, "\n") < 0)
25652566
goto write_err_out;
25662567
}
25672568
copy_begin = store.offset[i];
@@ -2570,18 +2571,17 @@ int git_config_set_multivar_in_file_gently(const char *config_filename,
25702571
/* write the pair (value == NULL means unset) */
25712572
if (value != NULL) {
25722573
if (store.state == START) {
2573-
if (!store_write_section(fd, key))
2574+
if (write_section(fd, key) < 0)
25742575
goto write_err_out;
25752576
}
2576-
if (!store_write_pair(fd, key, value))
2577+
if (write_pair(fd, key, value) < 0)
25772578
goto write_err_out;
25782579
}
25792580

25802581
/* write the rest of the config */
25812582
if (copy_begin < contents_sz)
25822583
if (write_in_full(fd, contents + copy_begin,
2583-
contents_sz - copy_begin) <
2584-
contents_sz - copy_begin)
2584+
contents_sz - copy_begin) < 0)
25852585
goto write_err_out;
25862586

25872587
munmap(contents, contents_sz);
@@ -2758,7 +2758,7 @@ int git_config_rename_section_in_file(const char *config_filename,
27582758
continue;
27592759
}
27602760
store.baselen = strlen(new_name);
2761-
if (!store_write_section(out_fd, new_name)) {
2761+
if (write_section(out_fd, new_name) < 0) {
27622762
ret = write_error(get_lock_file_path(lock));
27632763
goto out;
27642764
}
@@ -2784,7 +2784,7 @@ int git_config_rename_section_in_file(const char *config_filename,
27842784
if (remove)
27852785
continue;
27862786
length = strlen(output);
2787-
if (write_in_full(out_fd, output, length) != length) {
2787+
if (write_in_full(out_fd, output, length) < 0) {
27882788
ret = write_error(get_lock_file_path(lock));
27892789
goto out;
27902790
}

diff.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2978,7 +2978,7 @@ static void prep_temp_blob(const char *path, struct diff_tempfile *temp,
29782978
blob = buf.buf;
29792979
size = buf.len;
29802980
}
2981-
if (write_in_full(fd, blob, size) != size)
2981+
if (write_in_full(fd, blob, size) < 0)
29822982
die_errno("unable to write temp-file");
29832983
close_tempfile(&temp->tempfile);
29842984
temp->name = get_tempfile_path(&temp->tempfile);

entry.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,8 @@ static int write_entry(struct cache_entry *ce,
244244
char *new;
245245
struct strbuf buf = STRBUF_INIT;
246246
unsigned long size;
247-
size_t wrote, newsize = 0;
247+
ssize_t wrote;
248+
size_t newsize = 0;
248249
struct stat st;
249250
const struct submodule *sub;
250251

@@ -319,7 +320,7 @@ static int write_entry(struct cache_entry *ce,
319320
fstat_done = fstat_output(fd, state, &st);
320321
close(fd);
321322
free(new);
322-
if (wrote != size)
323+
if (wrote < 0)
323324
return error("unable to write file %s", path);
324325
break;
325326
case S_IFGITLINK:

fast-import.c

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

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

http-backend.c

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

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

@@ -378,7 +378,7 @@ static void copy_request(const char *prog_name, int out)
378378
ssize_t n = read_request(0, &buf);
379379
if (n < 0)
380380
die_errno("error reading request body");
381-
if (write_in_full(out, buf, n) != n)
381+
if (write_in_full(out, buf, n) < 0)
382382
die("%s aborted reading request", prog_name);
383383
close(out);
384384
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)