From 19134d0e15bcc7e73a3113705633a6800e8c5e17 Mon Sep 17 00:00:00 2001 From: Abdelrahman Said Date: Sun, 16 Feb 2025 14:55:17 +0000 Subject: [PATCH] Address Windows Spectre mitigation warnings --- src/core/strings/str8/str8.c | 39 ++++++++++---- tests/str8/test_str8.c | 98 +++++++++++++++++++++++++++--------- 2 files changed, 104 insertions(+), 33 deletions(-) diff --git a/src/core/strings/str8/str8.c b/src/core/strings/str8/str8.c index a3189a0..62da190 100644 --- a/src/core/strings/str8/str8.c +++ b/src/core/strings/str8/str8.c @@ -156,11 +156,18 @@ i64 wapp_str8_find(Str8RO *str, Str8RO substr) { return -1; } - for (u64 i = 0; i < str->size; ++i) { - const c8 *sub = str->buf + i; + // NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of + // MSVC Spectre mitigation warnings + u64 char_index = 0; + bool running = true; + while (running) { + const c8 *sub = str->buf + char_index; if (memcmp(sub, substr.buf, substr.size) == 0) { - return i; + return char_index; } + + ++char_index; + running = char_index < str->size; } return -1; @@ -171,11 +178,18 @@ i64 wapp_str8_rfind(Str8RO *str, Str8RO substr) { return -1; } - for (i64 i = str->size - substr.size; i >= 0; --i) { - const c8 *sub = str->buf + i; + // NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of + // MSVC Spectre mitigation warnings + i64 char_index = str->size - substr.size; + bool running = true; + while (running) { + const c8 *sub = str->buf + char_index; if (memcmp(sub, substr.buf, substr.size) == 0) { - return i; + return char_index; } + + --char_index; + running = char_index >= 0; } return -1; @@ -303,13 +317,20 @@ Str8 *wapp_str8_join(const Allocator *allocator, const Str8List *list, Str8RO *d u64 capacity = list->total_size + (delimiter->size * (list->node_count - 1)); Str8 *output = wapp_str8_buf_alloc(allocator, capacity); + // NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of + // MSVC Spectre mitigation warnings Str8Node *node; - for (u64 i = 0; i < list->node_count; ++i) { - node = wapp_str8_list_get(list, i); + u64 node_index = 0; + bool running = true; + while (running) { + node = wapp_str8_list_get(list, node_index); wapp_str8_concat_capped(output, node->string); - if (i + 1 < list->node_count) { + if (node_index + 1 < list->node_count) { wapp_str8_concat_capped(output, delimiter); } + + ++node_index; + running = node_index < list->node_count; } return output; diff --git a/tests/str8/test_str8.c b/tests/str8/test_str8.c index 79dd88e..2b23a7f 100644 --- a/tests/str8/test_str8.c +++ b/tests/str8/test_str8.c @@ -331,18 +331,35 @@ TestFuncResult test_str8_split(void) { wapp_str8_substr(&str, 16, 19), }; - u64 count1 = ARRLEN(splits1); - u64 count2 = ARRLEN(splits2); + u64 index1 = 0; + u64 count1 = ARRLEN(splits1); + bool running1 = true; + + u64 index2 = 0; + u64 count2 = ARRLEN(splits2); + bool running2 = true; result = list1->node_count == count1 && list1->total_size == str.size - 3; result = result && list2->node_count == count2 && list2->total_size == str.size - 4; - for (u64 i = 0; i < count1; ++i) { - Str8Node *node = wapp_str8_list_get(list1, i); - result = result && wapp_str8_equal(node->string, &(splits1[i])); + + // NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of + // MSVC Spectre mitigation warnings + while (running1) { + Str8Node *node = wapp_str8_list_get(list1, index1); + result = result && wapp_str8_equal(node->string, &(splits1[index1])); + + ++index1; + running1 = index1 < count1; } - for (u64 i = 0; i < count2; ++i) { - Str8Node *node = wapp_str8_list_get(list2, i); - result = result && wapp_str8_equal(node->string, &(splits2[i])); + + // NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of + // MSVC Spectre mitigation warnings + while (running2) { + Str8Node *node = wapp_str8_list_get(list2, index2); + result = result && wapp_str8_equal(node->string, &(splits2[index2])); + + ++index2; + running2 = index2 < count2; } wapp_mem_arena_allocator_destroy(&arena); @@ -364,12 +381,20 @@ TestFuncResult test_str8_split_with_max(void) { wapp_str8_substr(&str, 12, 19), }; - u64 count = ARRLEN(splits); + u64 index = 0; + u64 count = ARRLEN(splits); + bool running = true; result = list->node_count == count && list->total_size == str.size - 2; - for (u64 i = 0; i < count; ++i) { - Str8Node *node = wapp_str8_list_get(list, i); - result = result && wapp_str8_equal(node->string, &(splits[i])); + + // NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of + // MSVC Spectre mitigation warnings + while (running) { + Str8Node *node = wapp_str8_list_get(list, index); + result = result && wapp_str8_equal(node->string, &(splits[index])); + + ++index; + running = index < count; } wapp_mem_arena_allocator_destroy(&arena); @@ -398,18 +423,35 @@ TestFuncResult test_str8_rsplit(void) { wapp_str8_substr(&str, 16, 19), }; - u64 count1 = ARRLEN(splits1); - u64 count2 = ARRLEN(splits2); + u64 index1 = 0; + u64 count1 = ARRLEN(splits1); + bool running1 = true; + + u64 index2 = 0; + u64 count2 = ARRLEN(splits2); + bool running2 = true; result = list1->node_count == count1 && list1->total_size == str.size - 3; result = result && list2->node_count == count2 && list2->total_size == str.size - 4; - for (u64 i = 0; i < count1; ++i) { - Str8Node *node = wapp_str8_list_get(list1, i); - result = result && wapp_str8_equal(node->string, &(splits1[i])); + + // NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of + // MSVC Spectre mitigation warnings + while (running1) { + Str8Node *node = wapp_str8_list_get(list1, index1); + result = result && wapp_str8_equal(node->string, &(splits1[index1])); + + ++index1; + running1 = index1 < count1; } - for (u64 i = 0; i < count2; ++i) { - Str8Node *node = wapp_str8_list_get(list2, i); - result = result && wapp_str8_equal(node->string, &(splits2[i])); + + // NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of + // MSVC Spectre mitigation warnings + while (running2) { + Str8Node *node = wapp_str8_list_get(list2, index2); + result = result && wapp_str8_equal(node->string, &(splits2[index2])); + + ++index2; + running2 = index2 < count2; } wapp_mem_arena_allocator_destroy(&arena); @@ -431,12 +473,20 @@ TestFuncResult test_str8_rsplit_with_max(void) { wapp_str8_substr(&str, 17, 19), }; - u64 count = ARRLEN(splits); + u64 index = 0; + u64 count = ARRLEN(splits); + bool running = true; result = list->node_count == count && list->total_size == str.size - 2; - for (u64 i = 0; i < count; ++i) { - Str8Node *node = wapp_str8_list_get(list, i); - result = result && wapp_str8_equal(node->string, &(splits[i])); + + // NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of + // MSVC Spectre mitigation warnings + while (running) { + Str8Node *node = wapp_str8_list_get(list, index); + result = result && wapp_str8_equal(node->string, &(splits[index])); + + ++index; + running = index < count; } wapp_mem_arena_allocator_destroy(&arena);