Skip to content

Commit a846bff

Browse files
committed
Handle UBSan warnings about unaligned storing and reading
Fixes UBSan issues found in 8 - test_basic_parse (Failed) 9 - test_json_parser (Failed) 10 - test_json_printer (Failed) 11 - test_json (Failed) 12 - json_test_uql (Failed) 13 - json_test_uql_off (Failed) 14 - json_test_uq (Failed) 15 - json_test_uq_off (Failed) 16 - json_test (Failed) 19 - optional_scalars_test (Failed) Signed-off-by: Björn Svensson <[email protected]>
1 parent a04eff4 commit a846bff

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

include/flatcc/portable/pprintint.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,9 @@ static int print_int(int n, char *p);
102102
static int print_long(long n, char *p);
103103

104104

105-
#if defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64)
105+
/* The UBSan will warn about storing to misaligned address. */
106+
#if !(defined(__has_feature) && __has_feature(undefined_behavior_sanitizer)) && \
107+
(defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64))
106108
#define __print_unaligned_copy_16(p, q) (*(uint16_t*)(p) = *(uint16_t*)(q))
107109
#else
108110
#define __print_unaligned_copy_16(p, q) \

include/flatcc/portable/punaligned.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ extern "C" {
2828

2929
#ifndef PORTABLE_UNALIGNED_ACCESS
3030

31-
#if defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64)
31+
#if defined(__has_feature) && __has_feature(undefined_behavior_sanitizer)
32+
/* The UBSan will warn about unaligned access. */
33+
#define PORTABLE_UNALIGNED_ACCESS 0
34+
#elif defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64)
3235
#define PORTABLE_UNALIGNED_ACCESS 1
3336
#else
3437
#define PORTABLE_UNALIGNED_ACCESS 0

0 commit comments

Comments
 (0)