Show HN: FerroProve – Deterministic C-to-Rust lifter ( [forbid(unsafe_code)])
/* cJSON-style string unescape. Legacy defect: src[i + 1] is not bounds-checked. */
以下正文同步自 Hacker News 最新,版权归原站所有,已转换为易读排版。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
#include <stdint.h> #include <stddef.h>
/* cJSON-style string unescape. Legacy defect: src[i + 1] is not bounds-checked. */
static int g_trunc;
int hex_digit(char c) { if (c >= '0' && c <= '9') return c - '0'; if (c >= 'a' && c <= 'f') return c - 'a' + 10; if (c >= 'A' && c <= 'F') return c - 'A' + 10; return -1; }
int hex_byte(char hi, char lo) { int h1; int h2; h1 = hex_digit(hi); h2 = hex_digit(lo); if (h1 < 0 || h2 < 0) return -1; return h1 * 16 + h2; }
int scan_string(const char *src, size_t n, char *out, size_t outcap, size_t *written) { size_t i; size_t o; int c; int e; int hb; i = 0; o = 0; if (n == 0) return -1; if (src[0] != '"') return -1; i = 1; while (i < n && src[i] != '"') { c = src[i]; if (c == 92) { e = src[i + 1]; if (e == 'n') { c = 10; i = i + 2; } else if (e == 't') { c = 9; i = i + 2; } else if (e == 92) { c = e; i = i + 2; } else if (e == '"') { c = e; i = i + 2; } else if (e == 'x') { hb = hex_byte(src[i + 2], src[i + 3]); if (hb < 0) { g_trunc = g_trunc + 1; return -2; } c = hb; i = i + 4; } else { g_trunc = g_trunc + 1; return -2; } } else { i = i + 1; } if (o >= outcap) return -3; out[o] = c; o = o + 1; } if (i >= n) return -4; if (src[i] != '"') return -4; *written = o; return 0; }
CALL GRAPHverified order
hex_digithex_bytescan_string 7
L6 mutable global g_trunc
L33 unchecked index src[]
L35 unchecked index src[]
L36 unchecked index src[]
L38 unchecked index src[]
L52 unchecked index src[]
VERIFICATION GATEdone
1 Control-flow & call graphParsed 3 functions, 1 mutable global, 2 call edges. Verification order: hex_digit → hex_byte → scan_string.
Isolated g_trunc into explicit State struct (&mut State).
Synthesized safe slice & reference contracts across 3 functions. Selected entrypoint: scan_string.
Gate passed. #![forbid(unsafe_code)] holds. unsafe=0 todo=0 raw pointers=0. FFI shim is isolated in src/ffi.rs.
6 vectors. 5 byte-exact. 1 legacy out-of-range access contained as return -100. 0 mismatches.
正文由 探How 从来源站点 RSS 同步,内容未经改写;遇到排版缺失或需要图片、视频时请以原文为准。