Skip to content

Commit

Permalink
svf: fix compiler warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
tempname11 committed Nov 10, 2023
1 parent add4645 commit 5aef896
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 17 deletions.
1 change: 0 additions & 1 deletion library/src/platform/base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ static_assert(CHAR_BIT == 8);
static_assert(__LITTLE_ENDIAN__); // Clang-specific.
static_assert(sizeof(size_t) == sizeof(void *));
static_assert(sizeof(void *) >= 4);
static_assert(NULL == 0); // Probably unnecessary?

// Aliases for types we will use a lot, short and consistent.
// Like other types, these have uppercase first letter naming.
Expand Down
2 changes: 1 addition & 1 deletion svf_runtime/src/svf_compatibility.c
Original file line number Diff line number Diff line change
Expand Up @@ -1003,8 +1003,8 @@ void SVFRT_check_compatibility(
.field_matches_header = field_matches_header,
.field_matches = field_matches,
.option_matches_header = option_matches_header,
.option_matches = option_matches,
.option_matches_tags = option_matches_tags,
.option_matches = option_matches,
.current_level = sufficient_level,
.required_level = required_level,
.max_schema_work = max_schema_work,
Expand Down
5 changes: 3 additions & 2 deletions svf_runtime/src/svf_conversion.c
Original file line number Diff line number Diff line change
Expand Up @@ -1125,8 +1125,9 @@ void SVFRT_conversion_traverse_any_type(
return;
}

uint32_t data_offset = ~unsafe_representation_src.data_offset_complement;
uint64_t unsafe_end_offset_src = (
(uint64_t) ~unsafe_representation_src.data_offset_complement +
(uint64_t) data_offset +
(uint64_t) unsafe_representation_src.count * (uint64_t) unsafe_size_src
);

Expand Down Expand Up @@ -1305,7 +1306,7 @@ void SVFRT_convert_message(
return;
}

ctx->allocation.pointer = allocated_pointer;
ctx->allocation.pointer = (uint8_t *) allocated_pointer;
ctx->allocation.count = ctx->tally_dst;
out_result->output_bytes = ctx->allocation;

Expand Down
2 changes: 1 addition & 1 deletion svf_runtime/src/svf_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ typedef struct SVFRT_LogicalCompatibilityInfo {
} SVFRT_LogicalCompatibilityInfo;

typedef struct SVFRT_CompatibilityResult {
SVFRT_ErrorCode error_code; // See `SVFRT_code_compatibility__*`.
SVFRT_CompatibilityLevel level;
SVFRT_RangeU32 quirky_struct_strides_dst; // See #logical-compatibility-stride-quirk.
SVFRT_ErrorCode error_code; // See `SVFRT_code_compatibility__*`.

SVFRT_LogicalCompatibilityInfo logical; // Only for `SVFRT_compatibility_logical`.
} SVFRT_CompatibilityResult;
Expand Down
2 changes: 1 addition & 1 deletion svf_runtime/src/svf_runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ void SVFRT_read_message(
};
SVFRT_Bytes data_range = {
/*.pointer =*/ message.pointer + appendix_padded_end_offset,
/*.count =*/ message.count - appendix_padded_end_offset,
/*.count =*/ message.count - (uint32_t) appendix_padded_end_offset,
};

if (schema_range.count == 0) {
Expand Down
14 changes: 10 additions & 4 deletions svf_runtime/src/svf_runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include <climits>
#include <cstddef>
#include <cstdint>
#include <cstdbool>
#else
#include <limits.h>
#include <stddef.h>
Expand All @@ -22,6 +21,11 @@
extern "C" {
#endif

#if defined(_MSC_VER)
// MS: "All native scalar types are little-endian for the platforms that Microsoft Visual C++ targets (x86, x64, ARM, ARM64)."
#define SVF_PLATFORM_LITTLE_ENDIAN 1
#endif

#if defined(__clang__) && defined(__LITTLE_ENDIAN__)
#define SVF_PLATFORM_LITTLE_ENDIAN 1
#endif
Expand Down Expand Up @@ -382,10 +386,11 @@ void SVFRT_write_sequence_element(
uint32_t type_size,
SVFRT_Sequence *inout_sequence
) {
uint32_t data_offset = ~inout_sequence->data_offset_complement;
if (inout_sequence->count != 0) {
// Prevent multiply-add overflow by casting operands to `uint64_t` first. It
// works, because `UINT64_MAX == UINT32_MAX * UINT32_MAX + UINT32_MAX + UINT32_MAX`.
uint64_t end_offset = (uint64_t) (~inout_sequence->data_offset_complement) + (
uint64_t end_offset = (uint64_t) data_offset + (
(uint64_t) type_size * (uint64_t) inout_sequence->count
);

Expand Down Expand Up @@ -509,10 +514,11 @@ void const *SVFRT_read_sequence_element(
return NULL;
}

uint32_t data_offset = ~sequence.data_offset_complement;
// Prevent multiply-add overflow by casting operands to `uint64_t` first. It
// works, because `UINT64_MAX == UINT32_MAX * UINT32_MAX + UINT32_MAX + UINT32_MAX`.
uint64_t item_end_offset = (
(uint64_t) (~sequence.data_offset_complement) +
(uint64_t) data_offset +
(uint64_t) stride * ((uint64_t) element_index + 1)
);

Expand Down Expand Up @@ -546,7 +552,7 @@ void const *SVFRT_read_sequence_element(
(ctx), \
(void *) (array), \
sizeof(*(array)), \
sizeof(array) / sizeof(*(array)) - (termination_type == NULL ? 1 : 0) \
sizeof(array) / sizeof(*(array)) - (termination_type == 0 ? 1 : 0) \
)

#define SVFRT_WRITE_SEQUENCE_ELEMENT(ctx, data_ptr, inout_sequence) \
Expand Down
4 changes: 2 additions & 2 deletions svf_runtime/src/svf_runtime.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,13 +249,13 @@ static inline
Sequence<S> write_fixed_size_string(
WriteContext<E> *ctx,
T const (&array)[N],
bool termination_type = !NULL // Intended use: NULL for null-terminated strings, !NULL otherwise.
bool termination_type = !0 // Mnemonic: 0 for zero-terminated strings, !0 ("not zero") otherwise.
) noexcept {
static_assert(sizeof(typename IsPrimitive<S>::Yes) > 0);
static_assert(sizeof(S) == sizeof(T));
static_assert(N > 0);

return write_sequence(ctx, (S const *) array, (uint32_t) (termination_type == NULL ? N - 1 : N));
return write_sequence(ctx, (S const *) array, (uint32_t) (termination_type == 0 ? N - 1 : N));
}

template<typename T, typename E>
Expand Down
6 changes: 3 additions & 3 deletions svf_tools/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ endif()
#
# Common compiler options.
#
add_compile_options(-Wall -Wextra -Wpedantic)
add_compile_options(-Wall -Wextra -Wpedantic -Werror)
add_compile_options(-fno-exceptions -fno-rtti -nostdlib++)
include_directories(${CMAKE_CURRENT_BINARY_DIR}/custom)
include_directories(../library)
Expand Down Expand Up @@ -167,14 +167,15 @@ elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
endif()

#
# `svf_runtime`, compiled as C.
# `svf_runtime`, compiled as C99.
#
add_library(svf_runtime
../svf_runtime/src/svf_internal.c
../svf_runtime/src/svf_runtime.c
../svf_runtime/src/svf_compatibility.c
../svf_runtime/src/svf_conversion.c
)
target_compile_options(svf_runtime PRIVATE -std=c99 -pedantic-errors)

#
# `svfc`
Expand Down Expand Up @@ -301,7 +302,6 @@ add_dependencies(test_simple_d schema_D1_hpp)
add_executable(test_simple_c
src/test/simple/c.c
)
target_compile_options(test_simple_c PRIVATE)
add_dependencies(test_simple_c single_file_h)
add_dependencies(test_simple_c schema_A0_h)
add_dependencies(test_simple_c schema_A1_h)
Expand Down
4 changes: 2 additions & 2 deletions svf_tools/src/example/json_write.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ void example_write(FILE *file) {

svf::JSON::Field fields[2] = {};

fields[0].name = svf::runtime::write_fixed_size_string<uint8_t>(&ctx, "hello", NULL);
fields[0].name = svf::runtime::write_fixed_size_string<uint8_t>(&ctx, "hello", 0);
fields[0].value_tag = svf::JSON::Value_tag::number;
fields[0].value_payload.number = 42.0;

fields[1].name = svf::runtime::write_fixed_size_string<uint8_t>(&ctx, "world", NULL);
fields[1].name = svf::runtime::write_fixed_size_string<uint8_t>(&ctx, "world", 0);
fields[1].value_tag = svf::JSON::Value_tag::array;
for (int i = 0; i < 42; i++) {
svf::JSON::Item item = {};
Expand Down

0 comments on commit 5aef896

Please sign in to comment.