Skip to content

Commit cce81f2

Browse files
devdev
authored andcommitted
Fixed buffer overflow and bytes leaked not working correctly
1 parent 5fc8548 commit cce81f2

File tree

3 files changed

+21
-13
lines changed

3 files changed

+21
-13
lines changed

src/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ else()
4848
endif()
4949

5050
add_library(swiftnet STATIC ${SOURCE_FILES})
51-
target_compile_options(swiftnet PRIVATE -O3)
52-
target_link_options(swiftnet PRIVATE -O3)
51+
target_compile_options(swiftnet PRIVATE -O0)
52+
target_link_options(swiftnet PRIVATE -O0)
5353

5454
if(DEFINED ENV{VCPKG_ROOT} AND DEFINED CMAKE_VCPKG_TARGET_TRIPLET)
5555
set(VCPKG_INCLUDE_DIR "$ENV{VCPKG_ROOT}/installed/${CMAKE_VCPKG_TARGET_TRIPLET}/include")

src/internal/datatype_allocator.c

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -286,18 +286,24 @@ void allocator_destroy(struct SwiftNetMemoryAllocator* const memory_allocator) {
286286
break;
287287
}
288288

289-
free(current_stack);
290-
291289
#ifdef SWIFT_NET_DEBUG
292290
lock_ptr_status(current_stack);
293291

294-
for (uint32_t i = 0; i < sizeof(uint8_t) * (memory_allocator->chunk_item_amount / 8) + 1; i++) {
295-
if (current_stack->ptr_status + i != 0x00) {
296-
for(uint8_t bit = 0; bit < 8; bit++) {
297-
if((*(current_stack->ptr_status + i) & (1 << bit)) == 0x00) {
298-
bytes_leaked += memory_allocator->item_size;
299-
items_leaked++;
300-
}
292+
const uint32_t total_items = memory_allocator->chunk_item_amount;
293+
const uint32_t bytes = (total_items / 8) + 1;
294+
295+
for (uint32_t byte = 0; byte < bytes; byte++) {
296+
uint8_t mask = current_stack->ptr_status[byte];
297+
298+
for (uint8_t bit = 0; bit < 8; bit++) {
299+
uint32_t idx = byte * 8 + bit;
300+
if (idx >= total_items) break;
301+
302+
bool allocated = (mask & (1u << bit)) != 0;
303+
304+
if (allocated) {
305+
items_leaked++;
306+
bytes_leaked += memory_allocator->item_size;
301307
}
302308
}
303309
}
@@ -307,6 +313,8 @@ void allocator_destroy(struct SwiftNetMemoryAllocator* const memory_allocator) {
307313
free(current_stack->ptr_status);
308314
#endif
309315

316+
free(current_stack);
317+
310318
current_stack = next_stack;
311319
}
312320
}

tests/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ endif()
2121

2222
link_directories(${CMAKE_SOURCE_DIR}/../build/output)
2323

24-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -O3")
24+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -O0")
2525

2626
file(GLOB SRC_FILES "${CMAKE_SOURCE_DIR}/src/*.c")
2727

2828
add_executable(run_tests ${SRC_FILES})
2929

3030
target_link_libraries(run_tests PRIVATE swiftnet -lpcap)
31-
target_link_options(run_tests PRIVATE -g -O3)
31+
target_link_options(run_tests PRIVATE -g -O0)
3232

3333
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})

0 commit comments

Comments
 (0)