libarchive/libarchive — pull request #33 by xiw (2013-01-20 23:30:31+00:00)
Title: Fix overflow checking in archive_entry_sparse_add_entry()

gcc optimizes the overflow check `x + y < 0` (assuming x, y >= 0) into false, since signed integer overflow is undefined behavior in C.

Below is the simplified code.

``` c
#include

void bar(void);

void foo(int64_t offset, int64_t length)
{
if (offset < 0 || length < 0)
return;
if (offset + length < 0)
bar();
}
```

Run gcc with `-O2`.

```
$ gcc -S -o - -O2 t.c
...
foo:
.LFB0:
.cfi_startproc
rep ret
```

We can see that gcc optimizes away the check.

This patch uses a safe precondition check instead.