Skip to content

Commit 9631bfd

Browse files
authored
Bit of hardening (#11)
* Sanitizers Some UB elimination * Sanitize ci * gotta static your inlines * on second thought lets not sanitize in ci tis a silly place
1 parent 8017aec commit 9631bfd

File tree

5 files changed

+20
-20
lines changed

5 files changed

+20
-20
lines changed

Makefile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,3 @@ example: out/example
3030

3131
out:
3232
mkdir -p out
33-

ffc.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1711,13 +1711,15 @@ void ffc_sv_normalize(ffc_sv* sv) {
17111711

17121712
ffc_internal ffc_inline
17131713
uint64_t ffc_uint64_hi64_1(uint64_t r0, bool* truncated) {
1714+
FFC_DEBUG_ASSERT(r0 != 0);
17141715
*truncated = false;
17151716
int shl = (int)ffc_count_leading_zeroes(r0);
17161717
return r0 << shl;
17171718
}
17181719

17191720
ffc_internal ffc_inline
17201721
uint64_t ffc_uint64_hi64_2(uint64_t r0, uint64_t r1, bool* truncated) {
1722+
FFC_DEBUG_ASSERT(r0 != 0);
17211723
int shl = (int)ffc_count_leading_zeroes(r0);
17221724
if (shl == 0) {
17231725
*truncated = r1 != 0;
@@ -3121,28 +3123,28 @@ float ffc_parse_float_simple(size_t len, const char *s, ffc_outcome *outcome) {
31213123

31223124
ffc_result ffc_parse_i64(size_t len, const char *input, int base, int64_t *out) {
31233125
char *pend = (char*)(input + len);
3124-
ffc_int_value value_out;
3126+
ffc_int_value value_out = {0};
31253127
ffc_result result = ffc_parse_int_string(input, pend, &value_out, FFC_INT_KIND_S64, ffc_parse_options_default(), base);
31263128
*out = value_out.s64;
31273129
return result;
31283130
}
31293131
ffc_result ffc_parse_u64(size_t len, const char *input, int base, uint64_t *out) {
31303132
char *pend = (char*)(input + len);
3131-
ffc_int_value value_out;
3133+
ffc_int_value value_out = {0};
31323134
ffc_result result = ffc_parse_int_string(input, pend, &value_out, FFC_INT_KIND_U64, ffc_parse_options_default(), base);
31333135
*out = value_out.u64;
31343136
return result;
31353137
}
31363138
ffc_result ffc_parse_i32(size_t len, const char *input, int base, int32_t *out) {
31373139
char *pend = (char*)(input + len);
3138-
ffc_int_value value_out;
3140+
ffc_int_value value_out = {0};
31393141
ffc_result result = ffc_parse_int_string(input, pend, &value_out, FFC_INT_KIND_S32, ffc_parse_options_default(), base);
31403142
*out = value_out.s32;
31413143
return result;
31423144
}
31433145
ffc_result ffc_parse_u32(size_t len, const char *input, int base, uint32_t *out) {
31443146
char *pend = (char*)(input + len);
3145-
ffc_int_value value_out;
3147+
ffc_int_value value_out = {0};
31463148
ffc_result result = ffc_parse_int_string(input, pend, &value_out, FFC_INT_KIND_U32, ffc_parse_options_default(), base);
31473149
*out = value_out.u32;
31483150
return result;
@@ -3188,4 +3190,3 @@ ffc_result ffc_parse_u32(size_t len, const char *input, int base, uint32_t *out)
31883190

31893191
#endif /* FFC_H */
31903192

3191-

src/bigint.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,15 @@ void ffc_sv_normalize(ffc_sv* sv) {
158158

159159
ffc_internal ffc_inline
160160
uint64_t ffc_uint64_hi64_1(uint64_t r0, bool* truncated) {
161+
FFC_DEBUG_ASSERT(r0 != 0);
161162
*truncated = false;
162163
int shl = (int)ffc_count_leading_zeroes(r0);
163164
return r0 << shl;
164165
}
165166

166167
ffc_internal ffc_inline
167168
uint64_t ffc_uint64_hi64_2(uint64_t r0, uint64_t r1, bool* truncated) {
169+
FFC_DEBUG_ASSERT(r0 != 0);
168170
int shl = (int)ffc_count_leading_zeroes(r0);
169171
if (shl == 0) {
170172
*truncated = r1 != 0;

src/ffc.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -419,28 +419,28 @@ float ffc_parse_float_simple(size_t len, const char *s, ffc_outcome *outcome) {
419419

420420
ffc_result ffc_parse_i64(size_t len, const char *input, int base, int64_t *out) {
421421
char *pend = (char*)(input + len);
422-
ffc_int_value value_out;
422+
ffc_int_value value_out = {0};
423423
ffc_result result = ffc_parse_int_string(input, pend, &value_out, FFC_INT_KIND_S64, ffc_parse_options_default(), base);
424424
*out = value_out.s64;
425425
return result;
426426
}
427427
ffc_result ffc_parse_u64(size_t len, const char *input, int base, uint64_t *out) {
428428
char *pend = (char*)(input + len);
429-
ffc_int_value value_out;
429+
ffc_int_value value_out = {0};
430430
ffc_result result = ffc_parse_int_string(input, pend, &value_out, FFC_INT_KIND_U64, ffc_parse_options_default(), base);
431431
*out = value_out.u64;
432432
return result;
433433
}
434434
ffc_result ffc_parse_i32(size_t len, const char *input, int base, int32_t *out) {
435435
char *pend = (char*)(input + len);
436-
ffc_int_value value_out;
436+
ffc_int_value value_out = {0};
437437
ffc_result result = ffc_parse_int_string(input, pend, &value_out, FFC_INT_KIND_S32, ffc_parse_options_default(), base);
438438
*out = value_out.s32;
439439
return result;
440440
}
441441
ffc_result ffc_parse_u32(size_t len, const char *input, int base, uint32_t *out) {
442442
char *pend = (char*)(input + len);
443-
ffc_int_value value_out;
443+
ffc_int_value value_out = {0};
444444
ffc_result result = ffc_parse_int_string(input, pend, &value_out, FFC_INT_KIND_U32, ffc_parse_options_default(), base);
445445
*out = value_out.u32;
446446
return result;
@@ -485,4 +485,3 @@ ffc_result ffc_parse_u32(size_t len, const char *input, int base, uint32_t *out)
485485
#endif
486486

487487
#endif /* FFC_H */
488-

test_src/test.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ char *double_to_string(double d, char *buffer) {
3333
return buffer + written;
3434
}
3535

36-
inline ffc_outcome parse_outcome(uint64_t len, const char* outcome_text) {
36+
static inline ffc_outcome parse_outcome(uint64_t len, const char* outcome_text) {
3737
static const struct { const char *name; ffc_outcome val; } map[] = {
3838
{"ok", FFC_OUTCOME_OK},
3939
{"out_of_range", FFC_OUTCOME_OUT_OF_RANGE},
@@ -115,7 +115,7 @@ bool float_eq(float exp, float act) {
115115
};
116116
}
117117

118-
void assert_double(size_t len, char *input, double exp, double act) {
118+
void assert_double(size_t len, const char *input, double exp, double act) {
119119
if (!double_eq(exp, act)) {
120120
printf("\n\ninput: %.*s\n", (int)len, input);
121121
printf("\texp: %f\n\tact: %f\n\n", exp, act);
@@ -124,7 +124,7 @@ void assert_double(size_t len, char *input, double exp, double act) {
124124
}
125125
}
126126

127-
void assert_float(size_t len, char *input, float exp, float act) {
127+
void assert_float(size_t len, const char *input, float exp, float act) {
128128
if (!float_eq(exp, act)) {
129129
printf("\n\ninput: %.*s\n", (int)len, input);
130130
printf("\texp: %f\n\tact: %f\n\n", exp, act);
@@ -133,10 +133,10 @@ void assert_float(size_t len, char *input, float exp, float act) {
133133
}
134134
}
135135

136-
void verify_ext(size_t len, char input[len], ffc_value exp_value, ffc_value_kind vk, ffc_outcome exp_outcome, ffc_parse_options options) {
136+
void verify_ext(size_t len, const char *input, ffc_value exp_value, ffc_value_kind vk, ffc_outcome exp_outcome, ffc_parse_options options) {
137137
ffc_value value;
138138

139-
ffc_result result = ffc_from_chars(input, &input[len], options, &value, vk);
139+
ffc_result result = ffc_from_chars((char*)input, (char*)&input[len], options, &value, vk);
140140

141141
if (exp_outcome != result.outcome) {
142142
printf("\n\ninput: %.*s\n", (int)len, input);
@@ -158,19 +158,19 @@ void verify_ext(size_t len, char input[len], ffc_value exp_value, ffc_value_kind
158158
}
159159
}
160160

161-
void verify_double_ext(size_t len, char input[len], double exp_value, ffc_outcome exp_outcome, ffc_parse_options options) {
161+
void verify_double_ext(size_t len, const char *input, double exp_value, ffc_outcome exp_outcome, ffc_parse_options options) {
162162
ffc_value expected;
163163
expected.d = exp_value;
164164
verify_ext(len, input, expected, FFC_VALUE_KIND_DOUBLE, exp_outcome, options);
165165
}
166166

167-
void verify_float_ext(size_t len, char input[len], float exp_value, ffc_outcome exp_outcome, ffc_parse_options options) {
167+
void verify_float_ext(size_t len, const char *input, float exp_value, ffc_outcome exp_outcome, ffc_parse_options options) {
168168
ffc_value expected;
169169
expected.f = exp_value;
170170
verify_ext(len, input, expected, FFC_VALUE_KIND_FLOAT, exp_outcome, options);
171171
}
172172

173-
void verify_float(char *input, float exp_value) {
173+
void verify_float(const char *input, float exp_value) {
174174
verify_float_ext(strlen(input), input, exp_value, FFC_OUTCOME_OK, ffc_parse_options_default());
175175
}
176176

@@ -634,4 +634,3 @@ int main(void) {
634634

635635
return 0;
636636
}
637-

0 commit comments

Comments
 (0)