From f2f44dfce609b7f5b228e61829d454b5c8c2734a Mon Sep 17 00:00:00 2001 From: Aldrik Ramaekers Date: Thu, 14 Mar 2024 19:38:21 +0100 Subject: more static analysis fixes --- build_win32.bat | 6 +++++- imgui/backends/imgui_impl_win32.cpp | 4 ++-- src/array.cpp | 8 +++++++- src/config.cpp | 12 ++++++------ src/config.h | 1 + src/export.cpp | 2 +- src/import.cpp | 1 + src/main.cpp | 38 ++++++++++++++++++++----------------- src/memory_bucket.cpp | 4 ++++ 9 files changed, 48 insertions(+), 28 deletions(-) diff --git a/build_win32.bat b/build_win32.bat index cd1323f..f0bd716 100644 --- a/build_win32.bat +++ b/build_win32.bat @@ -7,12 +7,16 @@ call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary @set FLAGS= windres misc/icon.rc -O coff -o bin/debug/icon.res +if "%1"=="-a" ( + @set FLAGS=/analyze:external- /analyze:stacksize 40000 +) + if "%1"=="-release" ( @set OUT_DIR=bin\\release @set FLAGS=/GL /O2 /DTS_RELEASE ) mkdir %OUT_DIR% -cl /analyze /std:c++17 /nologo %FLAGS% /W3 /Zi /MD /EHsc /Isrc/windows /Iimgui /Iimgui/backends /Isrc /utf-8 %INCLUDES% /D UNICODE /D _UNICODE %SOURCES% /Fe%OUT_DIR%/%OUT_EXE%.exe /Fd%OUT_DIR%/vc140.pdb /Fo%OUT_DIR%/ /link %LIBS% +cl /std:c++17 /nologo %FLAGS% /W3 /Zi /MD /EHsc /Isrc/windows /external:W0 /external:Iimgui /external:Iimgui/backends /Isrc /utf-8 %INCLUDES% /D UNICODE /D _UNICODE %SOURCES% /Fe%OUT_DIR%/%OUT_EXE%.exe /Fd%OUT_DIR%/vc140.pdb /Fo%OUT_DIR%/ /link %LIBS% if "%1"=="-r" call "bin/debug/text-search.exe" if "%1"=="-d" call devenv "bin/debug/text-search.exe" diff --git a/imgui/backends/imgui_impl_win32.cpp b/imgui/backends/imgui_impl_win32.cpp index 73ed700..808f132 100644 --- a/imgui/backends/imgui_impl_win32.cpp +++ b/imgui/backends/imgui_impl_win32.cpp @@ -16,9 +16,9 @@ // - Documentation https://dearimgui.com/docs (same as your local docs/ folder). // - Introduction, links and more at the top of imgui.cpp -#include "imgui.h" +#include #ifndef IMGUI_DISABLE -#include "imgui_impl_win32.h" +#include #ifndef WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN #endif diff --git a/src/array.cpp b/src/array.cpp index fe91732..32eb4f3 100644 --- a/src/array.cpp +++ b/src/array.cpp @@ -28,6 +28,7 @@ uint32_t ts_array_push(ts_array *ts_array, void *data) if (!ts_array->data) { ts_array->data = malloc(ts_array->entry_size * ts_array->reserve_jump); + if (!ts_array->data) exit_oom(); ts_array->reserved_length = ts_array->reserve_jump; } @@ -35,6 +36,7 @@ uint32_t ts_array_push(ts_array *ts_array, void *data) { ts_array->reserved_length += ts_array->reserve_jump; ts_array->data = realloc(ts_array->data, (ts_array->reserved_length*ts_array->entry_size)); + if (!ts_array->data) exit_oom(); } memcpy((char*)ts_array->data + ((ts_array->length-1) * ts_array->entry_size), @@ -57,6 +59,7 @@ uint32_t ts_array_push_size(ts_array *ts_array, void *data, uint32_t data_size) if (!ts_array->data) { ts_array->data = malloc(ts_array->entry_size * ts_array->reserve_jump); + if (!ts_array->data) exit_oom(); ts_array->reserved_length = ts_array->reserve_jump; } @@ -64,6 +67,7 @@ uint32_t ts_array_push_size(ts_array *ts_array, void *data, uint32_t data_size) { ts_array->reserved_length += ts_array->reserve_jump; ts_array->data = realloc(ts_array->data, (ts_array->reserved_length*ts_array->entry_size)); + if (!ts_array->data) exit_oom(); } memcpy((char*)ts_array->data + ((ts_array->length-1) * ts_array->entry_size), @@ -97,10 +101,12 @@ void ts_array_reserve(ts_array *ts_array, uint32_t reserve_count) if (ts_array->data) { ts_array->data = realloc(ts_array->data, (ts_array->reserved_length*ts_array->entry_size)); + if (!ts_array->data) exit_oom(); } else { ts_array->data = malloc(ts_array->reserved_length*ts_array->entry_size); + if (!ts_array->data) exit_oom(); } } ts_mutex_unlock(&ts_array->mutex); @@ -180,7 +186,7 @@ ts_array ts_array_copy(ts_array *arr) new_ts_array.reserved_length = arr->reserved_length; new_ts_array.entry_size = arr->entry_size; new_ts_array.data = malloc(new_ts_array.entry_size*new_ts_array.reserved_length); - if (!new_bucket.data) exit_oom(); + if (!new_ts_array.data) exit_oom(); new_ts_array.mutex = ts_mutex_create(); diff --git a/src/config.cpp b/src/config.cpp index d7f1a43..017891a 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -16,16 +16,16 @@ bool respect_capitalization = false; static void _ts_config_ReadLine(ImGuiContext*, ImGuiSettingsHandler*, void* entry, const utf8_int8_t* line) { - utf8_int8_t path[MAX_INPUT_LENGTH]; - utf8_int8_t filter[MAX_INPUT_LENGTH]; - utf8_int8_t query[MAX_INPUT_LENGTH]; + utf8_int8_t path[MAX_INPUT_LENGTH] = {0}; + utf8_int8_t filter[MAX_INPUT_LENGTH] = {0}; + utf8_int8_t query[MAX_INPUT_LENGTH] = {0}; uint32_t threads = 1, maxSize = 100, matchCase = 0; #if defined(_WIN32) - if (sscanf_s(line, "Path=%s", (char*)&path, MAX_INPUT_LENGTH-1) == 1) { strncpy_s(path_buffer, MAX_INPUT_LENGTH, (char*)path, MAX_INPUT_LENGTH-1); } - else if (sscanf_s(line, "Filter=%s", (char*)&filter, MAX_INPUT_LENGTH-1) == 1) { strncpy_s(filter_buffer, MAX_INPUT_LENGTH, (char*)filter, MAX_INPUT_LENGTH-1); } - else if (sscanf_s(line, "Query=%s", (char*)&query, MAX_INPUT_LENGTH-1) == 1) { strncpy_s(query_buffer, MAX_INPUT_LENGTH, (char*)query, MAX_INPUT_LENGTH-1); } + if (sscanf_s(line, "Path=%s", (char*)&path, MAX_INPUT_LENGTH) == 1) { strncpy_s(path_buffer, MAX_INPUT_LENGTH, (char*)path, MAX_INPUT_LENGTH); } + else if (sscanf_s(line, "Filter=%s", (char*)&filter, MAX_INPUT_LENGTH) == 1) { strncpy_s(filter_buffer, MAX_INPUT_LENGTH, (char*)filter, MAX_INPUT_LENGTH); } + else if (sscanf_s(line, "Query=%s", (char*)&query, MAX_INPUT_LENGTH) == 1) { strncpy_s(query_buffer, MAX_INPUT_LENGTH, (char*)query, MAX_INPUT_LENGTH); } else if (sscanf_s(line, "Threads=%u", &threads) == 1) { ts_thread_count = threads; } else if (sscanf_s(line, "MaxSize=%u", &maxSize) == 1) { max_file_size = maxSize; } else if (sscanf_s(line, "MatchCase=%u", &matchCase) == 1) { respect_capitalization = matchCase; } diff --git a/src/config.h b/src/config.h index 20e63e9..65388c8 100644 --- a/src/config.h +++ b/src/config.h @@ -3,6 +3,7 @@ #if defined(_WIN32) #define MAX_INPUT_LENGTH 4096 #elif defined(__linux__) +#include #define MAX_INPUT_LENGTH 4096 #elif defined(__APPLE__) #define MAX_INPUT_LENGTH 1024 diff --git a/src/export.cpp b/src/export.cpp index 493c978..4e316c4 100644 --- a/src/export.cpp +++ b/src/export.cpp @@ -152,7 +152,7 @@ static bool _ts_export_csv(ts_search_result* result, const utf8_int8_t* path) { fprintf(write_file, "FILE,%s\n", match->file->path); } - fprintf(write_file, "MATCH,%d,%zu,%zu,%s\n", match->line_nr, match->word_match_length, match->word_match_offset, match->line_info); + fprintf(write_file, "MATCH,%u,%zu,%zu,%s\n", match->line_nr, match->word_match_length, match->word_match_offset, match->line_info); } fclose(write_file); diff --git a/src/import.cpp b/src/import.cpp index ffa187d..29f0ff9 100644 --- a/src/import.cpp +++ b/src/import.cpp @@ -67,6 +67,7 @@ static bool _ts_import_csv_v1(ts_search_result* result, FILE *read_file) { // New match within current_file if (current_file && sscanf(line_buffer, "MATCH,%u,%zu,%zu\n", &match.line_nr, &match.word_match_length, &match.word_match_offset) == 3) { match.file = current_file; + match.file->match_count++; utf8_int8_t* iter = line_buffer; int count = 0; diff --git a/src/main.cpp b/src/main.cpp index 16d918e..584f0a8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -331,10 +331,14 @@ void _ts_create_text_match_rows() { ts_found_file* prev_file = nullptr; for (uint32_t item = 0; item < itemcount; item++) { - ts_file_match *file = (ts_file_match *)ts_array_at(¤t_search_result->matches, item); + ts_file_match *match = (ts_file_match *)ts_array_at(¤t_search_result->matches, item); + if (match->file == NULL) { + assert(match->file); + continue; // Should never happen. + } - if (prev_file != file->file) { - prev_file = file->file; + if (prev_file != match->file) { + prev_file = match->file; char match_info_txt[20]; ImGui::TableNextRow(); @@ -342,24 +346,24 @@ void _ts_create_text_match_rows() { ImGui::SetCursorPosX(5); ImGui::PushStyleColor(ImGuiCol_Text, {0,0,0,0.1f}); - ImGui::TableHeader(file->file->collapsed ? "▶" : "▼"); + ImGui::TableHeader(match->file->collapsed ? "▶" : "▼"); ImGui::PopStyleColor(); ImGui::TableNextColumn(); - ImGui::TableHeader(file->file->path); + ImGui::TableHeader(match->file->path); ImGui::TableNextColumn(); - snprintf(match_info_txt, 20, "%u match(es)", file->file->match_count); + snprintf(match_info_txt, 20, "%u match(es)", match->file->match_count); ImGui::TableHeader(match_info_txt); ImGui::SameLine(); ImGui::Selectable("##nolabel", false, ImGuiSelectableFlags_SpanAllColumns | ImGuiSelectableFlags_AllowOverlap); if (ImGui::IsItemClicked(ImGuiPopupFlags_MouseButtonLeft)) { - file->file->collapsed = !file->file->collapsed; + match->file->collapsed = !match->file->collapsed; } } - if (file->file->collapsed) continue; + if (match->file->collapsed) continue; char match_nr[20]; snprintf(match_nr, 20, "#%u", item+1); @@ -371,7 +375,7 @@ void _ts_create_text_match_rows() { ImGui::TableNextColumn(); utf8_int32_t iter_ch = 0; - utf8_int8_t* iter = file->line_info; + utf8_int8_t* iter = match->line_info; size_t whitespace_size = 0; while ((iter = utf8codepoint(iter, &iter_ch)) && iter_ch) { @@ -385,11 +389,11 @@ void _ts_create_text_match_rows() { } } - ImGui::Text("%.*s", (int)(file->word_match_offset - whitespace_size), file->line_info + whitespace_size); + ImGui::Text("%.*s", (int)(match->word_match_offset - whitespace_size), match->line_info + whitespace_size); ImGui::SameLine(0.0f, 0.0f); - ImGui::TextColored({255,0,0,255}, "%.*s", (int)file->word_match_length, file->line_info + file->word_match_offset); + ImGui::TextColored({255,0,0,255}, "%.*s", (int)match->word_match_length, match->line_info + match->word_match_offset); ImGui::SameLine(0.0f, 0.0f); - ImGui::TextUnformatted(file->line_info + file->word_match_offset + file->word_match_length); + ImGui::TextUnformatted(match->line_info + match->word_match_offset + match->word_match_length); ImGui::SameLine(); ImGui::Selectable("##nolabel", false, ImGuiSelectableFlags_SpanAllColumns | ImGuiSelectableFlags_AllowOverlap); @@ -402,25 +406,25 @@ void _ts_create_text_match_rows() { #if defined(_WIN32) if (ImGui::MenuItem("Open as")) { - ts_platform_open_file_as(file->file->path); + ts_platform_open_file_as(match->file->path); } if (ImGui::MenuItem("Open folder")) { - ts_platform_open_file_in_folder(file->file->path); + ts_platform_open_file_in_folder(match->file->path); } #endif if (ImGui::MenuItem("Copy path")) { - ImGui::SetClipboardText(file->file->path); + ImGui::SetClipboardText(match->file->path); } if (ImGui::MenuItem("Copy line")) { - ImGui::SetClipboardText(file->line_info); + ImGui::SetClipboardText(match->line_info); } ImGui::EndPopup(); } ImGui::TableNextColumn(); - ImGui::Text("line %d", file->line_nr); + ImGui::Text("line %d", match->line_nr); } _ts_create_file_error_rows(); } diff --git a/src/memory_bucket.cpp b/src/memory_bucket.cpp index 952d342..d846bf1 100644 --- a/src/memory_bucket.cpp +++ b/src/memory_bucket.cpp @@ -32,6 +32,10 @@ void* ts_memory_bucket_reserve(ts_memory_bucket *bucket, uint32_t reserve_length return space; } + + if (bucket_entry == NULL) { + return NULL; // Should never happen. + } // failed to find suitable space, allocate new bucket ts_memory_bucket_entry new_bucket; -- cgit v1.2.3-70-g09d2