author | Lukas Fleischer <cgit@cryptocrack.de> | 2011-04-05 08:38:53 (UTC) |
---|---|---|
committer | Lars Hjemli <hjemli@gmail.com> | 2011-05-23 20:58:35 (UTC) |
commit | 9afc883297b0d0943e9b358d2299950f33e8e5ed (patch) (unidiff) | |
tree | 27e81428c0a6ad4bbdf5633fc95b946b4a631d30 /ui-diff.c | |
parent | a0bf375a1a9b74056a913f3687c6f5b42ad4acf6 (diff) | |
download | cgit-9afc883297b0d0943e9b358d2299950f33e8e5ed.zip cgit-9afc883297b0d0943e9b358d2299950f33e8e5ed.tar.gz cgit-9afc883297b0d0943e9b358d2299950f33e8e5ed.tar.bz2 |
Avoid null pointer dereference in cgit_print_diff().
When calling cgit_print_diff() with a bad new_rev and a NULL old_rev,
checking for new_rev's parent commit will result in a null pointer
dereference. Returning on an invalid commit before dereferencing fixes
this. Spotted with clang-analyzer.
Signed-off-by: Lukas Fleischer <cgit@cryptocrack.de>
Signed-off-by: Lars Hjemli <hjemli@gmail.com>
-rw-r--r-- | ui-diff.c | 8 |
1 files changed, 6 insertions, 2 deletions
@@ -252,135 +252,139 @@ static void header(unsigned char *sha1, char *path1, int mode1, | |||
252 | htmlf("<br/>deleted file mode %.6o", mode1); | 252 | htmlf("<br/>deleted file mode %.6o", mode1); |
253 | 253 | ||
254 | if (!subproject) { | 254 | if (!subproject) { |
255 | abbrev1 = xstrdup(find_unique_abbrev(sha1, DEFAULT_ABBREV)); | 255 | abbrev1 = xstrdup(find_unique_abbrev(sha1, DEFAULT_ABBREV)); |
256 | abbrev2 = xstrdup(find_unique_abbrev(sha2, DEFAULT_ABBREV)); | 256 | abbrev2 = xstrdup(find_unique_abbrev(sha2, DEFAULT_ABBREV)); |
257 | htmlf("<br/>index %s..%s", abbrev1, abbrev2); | 257 | htmlf("<br/>index %s..%s", abbrev1, abbrev2); |
258 | free(abbrev1); | 258 | free(abbrev1); |
259 | free(abbrev2); | 259 | free(abbrev2); |
260 | if (mode1 != 0 && mode2 != 0) { | 260 | if (mode1 != 0 && mode2 != 0) { |
261 | htmlf(" %.6o", mode1); | 261 | htmlf(" %.6o", mode1); |
262 | if (mode2 != mode1) | 262 | if (mode2 != mode1) |
263 | htmlf("..%.6o", mode2); | 263 | htmlf("..%.6o", mode2); |
264 | } | 264 | } |
265 | html("<br/>--- a/"); | 265 | html("<br/>--- a/"); |
266 | if (mode1 != 0) | 266 | if (mode1 != 0) |
267 | cgit_tree_link(path1, NULL, NULL, ctx.qry.head, | 267 | cgit_tree_link(path1, NULL, NULL, ctx.qry.head, |
268 | sha1_to_hex(old_rev_sha1), path1); | 268 | sha1_to_hex(old_rev_sha1), path1); |
269 | else | 269 | else |
270 | html_txt(path1); | 270 | html_txt(path1); |
271 | html("<br/>+++ b/"); | 271 | html("<br/>+++ b/"); |
272 | if (mode2 != 0) | 272 | if (mode2 != 0) |
273 | cgit_tree_link(path2, NULL, NULL, ctx.qry.head, | 273 | cgit_tree_link(path2, NULL, NULL, ctx.qry.head, |
274 | sha1_to_hex(new_rev_sha1), path2); | 274 | sha1_to_hex(new_rev_sha1), path2); |
275 | else | 275 | else |
276 | html_txt(path2); | 276 | html_txt(path2); |
277 | } | 277 | } |
278 | html("</div>"); | 278 | html("</div>"); |
279 | } | 279 | } |
280 | 280 | ||
281 | static void print_ssdiff_link() | 281 | static void print_ssdiff_link() |
282 | { | 282 | { |
283 | if (!strcmp(ctx.qry.page, "diff")) { | 283 | if (!strcmp(ctx.qry.page, "diff")) { |
284 | if (use_ssdiff) | 284 | if (use_ssdiff) |
285 | cgit_diff_link("Unidiff", NULL, NULL, ctx.qry.head, | 285 | cgit_diff_link("Unidiff", NULL, NULL, ctx.qry.head, |
286 | ctx.qry.sha1, ctx.qry.sha2, ctx.qry.path, 1); | 286 | ctx.qry.sha1, ctx.qry.sha2, ctx.qry.path, 1); |
287 | else | 287 | else |
288 | cgit_diff_link("Side-by-side diff", NULL, NULL, | 288 | cgit_diff_link("Side-by-side diff", NULL, NULL, |
289 | ctx.qry.head, ctx.qry.sha1, | 289 | ctx.qry.head, ctx.qry.sha1, |
290 | ctx.qry.sha2, ctx.qry.path, 1); | 290 | ctx.qry.sha2, ctx.qry.path, 1); |
291 | } | 291 | } |
292 | } | 292 | } |
293 | 293 | ||
294 | static void filepair_cb(struct diff_filepair *pair) | 294 | static void filepair_cb(struct diff_filepair *pair) |
295 | { | 295 | { |
296 | unsigned long old_size = 0; | 296 | unsigned long old_size = 0; |
297 | unsigned long new_size = 0; | 297 | unsigned long new_size = 0; |
298 | int binary = 0; | 298 | int binary = 0; |
299 | linediff_fn print_line_fn = print_line; | 299 | linediff_fn print_line_fn = print_line; |
300 | 300 | ||
301 | current_filepair = pair; | 301 | current_filepair = pair; |
302 | if (use_ssdiff) { | 302 | if (use_ssdiff) { |
303 | cgit_ssdiff_header_begin(); | 303 | cgit_ssdiff_header_begin(); |
304 | print_line_fn = cgit_ssdiff_line_cb; | 304 | print_line_fn = cgit_ssdiff_line_cb; |
305 | } | 305 | } |
306 | header(pair->one->sha1, pair->one->path, pair->one->mode, | 306 | header(pair->one->sha1, pair->one->path, pair->one->mode, |
307 | pair->two->sha1, pair->two->path, pair->two->mode); | 307 | pair->two->sha1, pair->two->path, pair->two->mode); |
308 | if (use_ssdiff) | 308 | if (use_ssdiff) |
309 | cgit_ssdiff_header_end(); | 309 | cgit_ssdiff_header_end(); |
310 | if (S_ISGITLINK(pair->one->mode) || S_ISGITLINK(pair->two->mode)) { | 310 | if (S_ISGITLINK(pair->one->mode) || S_ISGITLINK(pair->two->mode)) { |
311 | if (S_ISGITLINK(pair->one->mode)) | 311 | if (S_ISGITLINK(pair->one->mode)) |
312 | print_line_fn(fmt("-Subproject %s", sha1_to_hex(pair->one->sha1)), 52); | 312 | print_line_fn(fmt("-Subproject %s", sha1_to_hex(pair->one->sha1)), 52); |
313 | if (S_ISGITLINK(pair->two->mode)) | 313 | if (S_ISGITLINK(pair->two->mode)) |
314 | print_line_fn(fmt("+Subproject %s", sha1_to_hex(pair->two->sha1)), 52); | 314 | print_line_fn(fmt("+Subproject %s", sha1_to_hex(pair->two->sha1)), 52); |
315 | if (use_ssdiff) | 315 | if (use_ssdiff) |
316 | cgit_ssdiff_footer(); | 316 | cgit_ssdiff_footer(); |
317 | return; | 317 | return; |
318 | } | 318 | } |
319 | if (cgit_diff_files(pair->one->sha1, pair->two->sha1, &old_size, | 319 | if (cgit_diff_files(pair->one->sha1, pair->two->sha1, &old_size, |
320 | &new_size, &binary, ctx.qry.context, | 320 | &new_size, &binary, ctx.qry.context, |
321 | ctx.qry.ignorews, print_line_fn)) | 321 | ctx.qry.ignorews, print_line_fn)) |
322 | cgit_print_error("Error running diff"); | 322 | cgit_print_error("Error running diff"); |
323 | if (binary) { | 323 | if (binary) { |
324 | if (use_ssdiff) | 324 | if (use_ssdiff) |
325 | html("<tr><td colspan='4'>Binary files differ</td></tr>"); | 325 | html("<tr><td colspan='4'>Binary files differ</td></tr>"); |
326 | else | 326 | else |
327 | html("Binary files differ"); | 327 | html("Binary files differ"); |
328 | } | 328 | } |
329 | if (use_ssdiff) | 329 | if (use_ssdiff) |
330 | cgit_ssdiff_footer(); | 330 | cgit_ssdiff_footer(); |
331 | } | 331 | } |
332 | 332 | ||
333 | void cgit_print_diff(const char *new_rev, const char *old_rev, const char *prefix) | 333 | void cgit_print_diff(const char *new_rev, const char *old_rev, const char *prefix) |
334 | { | 334 | { |
335 | enum object_type type; | 335 | enum object_type type; |
336 | unsigned long size; | 336 | unsigned long size; |
337 | struct commit *commit, *commit2; | 337 | struct commit *commit, *commit2; |
338 | 338 | ||
339 | if (!new_rev) | 339 | if (!new_rev) |
340 | new_rev = ctx.qry.head; | 340 | new_rev = ctx.qry.head; |
341 | get_sha1(new_rev, new_rev_sha1); | 341 | get_sha1(new_rev, new_rev_sha1); |
342 | type = sha1_object_info(new_rev_sha1, &size); | 342 | type = sha1_object_info(new_rev_sha1, &size); |
343 | if (type == OBJ_BAD) { | 343 | if (type == OBJ_BAD) { |
344 | cgit_print_error(fmt("Bad object name: %s", new_rev)); | 344 | cgit_print_error(fmt("Bad object name: %s", new_rev)); |
345 | return; | 345 | return; |
346 | } | 346 | } |
347 | commit = lookup_commit_reference(new_rev_sha1); | 347 | commit = lookup_commit_reference(new_rev_sha1); |
348 | if (!commit || parse_commit(commit)) | 348 | if (!commit || parse_commit(commit)) { |
349 | cgit_print_error(fmt("Bad commit: %s", sha1_to_hex(new_rev_sha1))); | 349 | cgit_print_error(fmt("Bad commit: %s", sha1_to_hex(new_rev_sha1))); |
350 | return; | ||
351 | } | ||
350 | 352 | ||
351 | if (old_rev) | 353 | if (old_rev) |
352 | get_sha1(old_rev, old_rev_sha1); | 354 | get_sha1(old_rev, old_rev_sha1); |
353 | else if (commit->parents && commit->parents->item) | 355 | else if (commit->parents && commit->parents->item) |
354 | hashcpy(old_rev_sha1, commit->parents->item->object.sha1); | 356 | hashcpy(old_rev_sha1, commit->parents->item->object.sha1); |
355 | else | 357 | else |
356 | hashclr(old_rev_sha1); | 358 | hashclr(old_rev_sha1); |
357 | 359 | ||
358 | if (!is_null_sha1(old_rev_sha1)) { | 360 | if (!is_null_sha1(old_rev_sha1)) { |
359 | type = sha1_object_info(old_rev_sha1, &size); | 361 | type = sha1_object_info(old_rev_sha1, &size); |
360 | if (type == OBJ_BAD) { | 362 | if (type == OBJ_BAD) { |
361 | cgit_print_error(fmt("Bad object name: %s", sha1_to_hex(old_rev_sha1))); | 363 | cgit_print_error(fmt("Bad object name: %s", sha1_to_hex(old_rev_sha1))); |
362 | return; | 364 | return; |
363 | } | 365 | } |
364 | commit2 = lookup_commit_reference(old_rev_sha1); | 366 | commit2 = lookup_commit_reference(old_rev_sha1); |
365 | if (!commit2 || parse_commit(commit2)) | 367 | if (!commit2 || parse_commit(commit2)) { |
366 | cgit_print_error(fmt("Bad commit: %s", sha1_to_hex(old_rev_sha1))); | 368 | cgit_print_error(fmt("Bad commit: %s", sha1_to_hex(old_rev_sha1))); |
369 | return; | ||
370 | } | ||
367 | } | 371 | } |
368 | 372 | ||
369 | if ((ctx.qry.ssdiff && !ctx.cfg.ssdiff) || (!ctx.qry.ssdiff && ctx.cfg.ssdiff)) | 373 | if ((ctx.qry.ssdiff && !ctx.cfg.ssdiff) || (!ctx.qry.ssdiff && ctx.cfg.ssdiff)) |
370 | use_ssdiff = 1; | 374 | use_ssdiff = 1; |
371 | 375 | ||
372 | print_ssdiff_link(); | 376 | print_ssdiff_link(); |
373 | cgit_print_diffstat(old_rev_sha1, new_rev_sha1, prefix); | 377 | cgit_print_diffstat(old_rev_sha1, new_rev_sha1, prefix); |
374 | 378 | ||
375 | if (use_ssdiff) { | 379 | if (use_ssdiff) { |
376 | html("<table summary='ssdiff' class='ssdiff'>"); | 380 | html("<table summary='ssdiff' class='ssdiff'>"); |
377 | } else { | 381 | } else { |
378 | html("<table summary='diff' class='diff'>"); | 382 | html("<table summary='diff' class='diff'>"); |
379 | html("<tr><td>"); | 383 | html("<tr><td>"); |
380 | } | 384 | } |
381 | cgit_diff_tree(old_rev_sha1, new_rev_sha1, filepair_cb, prefix, | 385 | cgit_diff_tree(old_rev_sha1, new_rev_sha1, filepair_cb, prefix, |
382 | ctx.qry.ignorews); | 386 | ctx.qry.ignorews); |
383 | if (!use_ssdiff) | 387 | if (!use_ssdiff) |
384 | html("</td></tr>"); | 388 | html("</td></tr>"); |
385 | html("</table>"); | 389 | html("</table>"); |
386 | } | 390 | } |