Skip to content

Commit 9308b7f

Browse files
mhaggergitster
authored andcommitted
read_packed_refs(): die if packed-refs contains bogus data
The old code ignored any lines that it didn't understand, including unterminated lines. This is dangerous. Instead, `die()` if the `packed-refs` file contains any unterminated lines or lines that we don't know how to handle. This fixes the tests added in the last commit. Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 02a1a42 commit 9308b7f

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

refs/packed-backend.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,9 @@ static struct packed_ref_cache *read_packed_refs(const char *packed_refs_file)
229229
const char *refname;
230230
const char *traits;
231231

232+
if (!line.len || line.buf[line.len - 1] != '\n')
233+
die("unterminated line in %s: %s", packed_refs_file, line.buf);
234+
232235
if (skip_prefix(line.buf, "# pack-refs with:", &traits)) {
233236
if (strstr(traits, " fully-peeled "))
234237
peeled = PEELED_FULLY;
@@ -253,9 +256,7 @@ static struct packed_ref_cache *read_packed_refs(const char *packed_refs_file)
253256
(peeled == PEELED_TAGS && starts_with(refname, "refs/tags/")))
254257
last->flag |= REF_KNOWS_PEELED;
255258
add_ref_entry(dir, last);
256-
continue;
257-
}
258-
if (last &&
259+
} else if (last &&
259260
line.buf[0] == '^' &&
260261
line.len == PEELED_LINE_LENGTH &&
261262
line.buf[PEELED_LINE_LENGTH - 1] == '\n' &&
@@ -267,6 +268,9 @@ static struct packed_ref_cache *read_packed_refs(const char *packed_refs_file)
267268
* reference:
268269
*/
269270
last->flag |= REF_KNOWS_PEELED;
271+
} else {
272+
strbuf_setlen(&line, line.len - 1);
273+
die("unexpected line in %s: %s", packed_refs_file, line.buf);
270274
}
271275
}
272276

t/t3210-pack-refs.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ test_expect_success 'notice d/f conflict with existing ref' '
194194
test_must_fail git branch foo/bar/baz/lots/of/extra/components
195195
'
196196

197-
test_expect_failure 'reject packed-refs with unterminated line' '
197+
test_expect_success 'reject packed-refs with unterminated line' '
198198
cp .git/packed-refs .git/packed-refs.bak &&
199199
test_when_finished "mv .git/packed-refs.bak .git/packed-refs" &&
200200
printf "%s" "$HEAD refs/zzzzz" >>.git/packed-refs &&
@@ -203,7 +203,7 @@ test_expect_failure 'reject packed-refs with unterminated line' '
203203
test_cmp expected_err err
204204
'
205205

206-
test_expect_failure 'reject packed-refs containing junk' '
206+
test_expect_success 'reject packed-refs containing junk' '
207207
cp .git/packed-refs .git/packed-refs.bak &&
208208
test_when_finished "mv .git/packed-refs.bak .git/packed-refs" &&
209209
printf "%s\n" "bogus content" >>.git/packed-refs &&
@@ -212,7 +212,7 @@ test_expect_failure 'reject packed-refs containing junk' '
212212
test_cmp expected_err err
213213
'
214214

215-
test_expect_failure 'reject packed-refs with a short SHA-1' '
215+
test_expect_success 'reject packed-refs with a short SHA-1' '
216216
cp .git/packed-refs .git/packed-refs.bak &&
217217
test_when_finished "mv .git/packed-refs.bak .git/packed-refs" &&
218218
printf "%.7s %s\n" $HEAD refs/zzzzz >>.git/packed-refs &&

0 commit comments

Comments
 (0)