Skip to content

Commit d8ff9f6

Browse files
mhaggerGit for Windows Build Agent
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a566839 commit d8ff9f6

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
@@ -230,6 +230,9 @@ static struct packed_ref_cache *read_packed_refs(const char *packed_refs_file)
230230
const char *refname;
231231
const char *traits;
232232

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

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)