Skip to content

Commit e6c1883

Browse files
committed
fix: use union-based cast to resolve const-correctness without warnings
The previous cast approach triggered -Wcast-qual warnings in CI. This implementation uses a union to safely convert const to non-const pointer, which is a standard C technique that avoids compiler warnings. How it works: - Data array is const (preventing modification) - Union allows type-punning between const and non-const pointers - Accessing via .ptr member gives non-const pointer for struct assignment - No undefined behavior as per C standard union rules This maintains const-correctness of the underlying data while being compatible with struct s2n_blob's non-const data pointer member.
1 parent 131f3f2 commit e6c1883

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

utils/s2n_blob.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ int S2N_RESULT_MUST_USE s2n_blob_slice(const struct s2n_blob *b, struct s2n_blob
6969
struct s2n_blob name = { 0 }; \
7070
RESULT_GUARD_POSIX(s2n_blob_init(&name, name##_buf, name##_requested_size))
7171

72-
#define S2N_BLOB_LABEL(name, str) \
73-
static const uint8_t name##_data[] = str; \
74-
const struct s2n_blob name = { .data = (uint8_t *) name##_data, .size = sizeof(name##_data) - 1 };
72+
#define S2N_BLOB_LABEL(name, str) \
73+
static const uint8_t name##_data[] = str; \
74+
static union { \
75+
const uint8_t *const_ptr; \
76+
uint8_t *ptr; \
77+
} name##_union = { .const_ptr = name##_data }; \
78+
const struct s2n_blob name = { .data = name##_union.ptr, .size = sizeof(name##_data) - 1 };

0 commit comments

Comments
 (0)