Skip to content

Commit

Permalink
Resolve UBSAN issues
Browse files Browse the repository at this point in the history
Silence and fix unsigned overflows reported by clang's UBSAN.
  • Loading branch information
cgzones committed May 25, 2021
1 parent 2d47c27 commit 297e89b
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/khash.h
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,9 @@ static const double __ac_HASH_UPPER = 0.77;
*/
#if defined(__clang__) && defined(__clang_major__) && (__clang_major__ >= 4)
__attribute__((no_sanitize ("unsigned-integer-overflow")))
#if (__clang_major__ >= 12)
__attribute__((no_sanitize("unsigned-shift-base")))
#endif
#endif
static kh_inline khint_t __ac_X31_hash_string (const char *s) {
khint_t h = (khint_t) * s;
Expand Down
2 changes: 1 addition & 1 deletion src/pdjson.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ init (json_stream * json) {
json->next = (enum json_type) 0;

json->stack = NULL;
json->stack_top = -1;
json->stack_top = (size_t) -1;
json->stack_size = 0;

json->data.string = NULL;
Expand Down
6 changes: 6 additions & 0 deletions src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,12 @@ count_matches (const char *s1, char c) {
}

/* Simple but efficient uint32_t hashing. */
#if defined(__clang__) && defined(__clang_major__) && (__clang_major__ >= 4)
__attribute__((no_sanitize("unsigned-integer-overflow")))
#if (__clang_major__ >= 12)
__attribute__((no_sanitize("unsigned-shift-base")))
#endif
#endif
uint32_t
djb2 (unsigned char *str) {
uint32_t hash = 5381;
Expand Down

0 comments on commit 297e89b

Please sign in to comment.