Skip to content

Commit fb89520

Browse files
rscharfegitster
authored andcommitted
fsck: use strbuf_getline() to read skiplist file
The buffer is unlikely to contain a NUL character, so printing its contents using %s in a die() format is unsafe (detected with ASan). Use an idiomatic strbuf_getline() loop instead, which ensures the buffer is always NUL-terminated, supports CRLF files as well, accepts files without a newline after the last line, supports any hash length automatically, and is shorter. This fixes a bug where emitting an error about an invalid line on say line 1 would continue printing subsequent lines, and usually continue into uninitialized memory. The performance impact of this, on a CentOS 7 box with RedHat GCC 4.8.5-28: $ GIT_PERF_REPEAT_COUNT=5 GIT_PERF_MAKE_OPTS='-j56 CFLAGS="-O3"' ./run HEAD~ HEAD p1451-fsck-skip-list.sh Test HEAD~ HEAD ---------------------------------------------------------------------------------------- 1450.3: fsck with 0 skipped bad commits 7.75(7.39+0.35) 7.68(7.29+0.39) -0.9% 1450.5: fsck with 1 skipped bad commits 7.70(7.30+0.40) 7.80(7.42+0.37) +1.3% 1450.7: fsck with 10 skipped bad commits 7.77(7.37+0.40) 7.87(7.47+0.40) +1.3% 1450.9: fsck with 100 skipped bad commits 7.82(7.41+0.40) 7.88(7.43+0.44) +0.8% 1450.11: fsck with 1000 skipped bad commits 7.88(7.49+0.39) 7.84(7.43+0.40) -0.5% 1450.13: fsck with 10000 skipped bad commits 8.02(7.63+0.39) 8.07(7.67+0.39) +0.6% 1450.15: fsck with 100000 skipped bad commits 8.01(7.60+0.41) 8.08(7.70+0.38) +0.9% 1450.17: fsck with 1000000 skipped bad commits 7.60(7.10+0.50) 7.37(7.18+0.19) -3.0% Helped-by: Jeff King <[email protected]> Signed-off-by: Rene Scharfe <[email protected]> Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 01e0d54 commit fb89520

File tree

2 files changed

+13
-14
lines changed

2 files changed

+13
-14
lines changed

fsck.c

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,9 @@ static int fsck_msg_type(enum fsck_msg_id msg_id,
183183
static void init_skiplist(struct fsck_options *options, const char *path)
184184
{
185185
static struct oid_array skiplist = OID_ARRAY_INIT;
186-
int sorted, fd;
187-
char buffer[GIT_MAX_HEXSZ + 1];
186+
int sorted;
187+
FILE *fp;
188+
struct strbuf sb = STRBUF_INIT;
188189
struct object_id oid;
189190

190191
if (options->skiplist)
@@ -194,25 +195,23 @@ static void init_skiplist(struct fsck_options *options, const char *path)
194195
options->skiplist = &skiplist;
195196
}
196197

197-
fd = open(path, O_RDONLY);
198-
if (fd < 0)
198+
fp = fopen(path, "r");
199+
if (!fp)
199200
die("Could not open skip list: %s", path);
200-
for (;;) {
201+
while (!strbuf_getline(&sb, fp)) {
201202
const char *p;
202-
int result = read_in_full(fd, buffer, sizeof(buffer));
203-
if (result < 0)
204-
die_errno("Could not read '%s'", path);
205-
if (!result)
206-
break;
207-
if (parse_oid_hex(buffer, &oid, &p) || *p != '\n')
208-
die("Invalid SHA-1: %s", buffer);
203+
if (parse_oid_hex(sb.buf, &oid, &p) || *p != '\0')
204+
die("Invalid SHA-1: %s", sb.buf);
209205
oid_array_append(&skiplist, &oid);
210206
if (sorted && skiplist.nr > 1 &&
211207
oidcmp(&skiplist.oid[skiplist.nr - 2],
212208
&oid) > 0)
213209
sorted = 0;
214210
}
215-
close(fd);
211+
if (ferror(fp))
212+
die_errno("Could not read '%s'", path);
213+
fclose(fp);
214+
strbuf_release(&sb);
216215

217216
if (sorted)
218217
skiplist.sorted = 1;

t/t5504-fetch-receive-strict.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ test_expect_success 'fsck with invalid or bogus skipList input (comments & empty
185185
test_i18ngrep "^fatal: Invalid SHA-1: " err-with-empty-line
186186
'
187187

188-
test_expect_failure 'fsck no garbage output from comments & empty lines errors' '
188+
test_expect_success 'fsck no garbage output from comments & empty lines errors' '
189189
test_line_count = 1 err-with-comment &&
190190
test_line_count = 1 err-with-empty-line
191191
'

0 commit comments

Comments
 (0)