|
| 1 | +/* |
| 2 | + * Small reproducer for the CodeHawk-C my_regexec failure seen in SPEC |
| 3 | + * CPU2017 perlbench. |
| 4 | + * |
| 5 | + * The important shape is: |
| 6 | + * |
| 7 | + * unsigned_len_arg(..., end - begin) |
| 8 | + * |
| 9 | + * where begin and end are both char pointers. In C, end - begin is pointer |
| 10 | + * subtraction with signed ptrdiff_t result. Passing it to an unsigned length |
| 11 | + * parameter creates the same kind of signed-to-unsigned-cast proof obligation |
| 12 | + * as perlbench's: |
| 13 | + * |
| 14 | + * MgBYTEPOS(mg, sv, strbeg, strend - strbeg) |
| 15 | + * |
| 16 | + * CH-C generated a valid obligation for that cast, but then crashed while |
| 17 | + * reconstructing an API expression for the upper-bound invariant because it |
| 18 | + * treated the pointer difference as ordinary arithmetic XMinus and attempted |
| 19 | + * integer promotion on (char *) and (char *). |
| 20 | + * |
| 21 | + * Ref: https://github.com/static-analysis-engineering/CodeHawk-C/issues/75 |
| 22 | + */ |
| 23 | + |
| 24 | +#include <stddef.h> |
| 25 | + |
| 26 | +typedef unsigned long STRLEN; |
| 27 | + |
| 28 | +static STRLEN consume_len(const char *base, STRLEN len) { |
| 29 | + return base[len] == '\0' ? len : len + 1; |
| 30 | +} |
| 31 | + |
| 32 | +STRLEN my_regexec_repro(const char *strbeg, const char *strend) { |
| 33 | + return consume_len(strbeg, strend - strbeg); |
| 34 | +} |
| 35 | + |
| 36 | +STRLEN my_regexec_repro_with_guard(const char *strbeg, const char *strend) { |
| 37 | + if (strend < strbeg) { |
| 38 | + return 0; |
| 39 | + } |
| 40 | + |
| 41 | + return consume_len(strbeg, strend - strbeg); |
| 42 | +} |
| 43 | + |
| 44 | +STRLEN my_regexec_source_workaround(const char *strbeg, const char *strend) { |
| 45 | + if (strend < strbeg) { |
| 46 | + return 0; |
| 47 | + } |
| 48 | + |
| 49 | + { |
| 50 | + STRLEN len = (STRLEN)(strend - strbeg); |
| 51 | + return consume_len(strbeg, len); |
| 52 | + } |
| 53 | +} |
0 commit comments