fix: most lint issues

This commit is contained in:
Krzysztof Rudnicki 2025-07-14 16:25:23 +02:00
parent 0fc908f92d
commit 2be0f1f8a9
3 changed files with 205 additions and 173 deletions

6
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,6 @@
{
"files.associations": {
"*.py": "python",
"stdio.h": "c"
}
}

View File

@ -86,8 +86,8 @@ run_clang_tidy() {
run_cppcheck() { run_cppcheck() {
print_step "Running cppcheck analysis..." print_step "Running cppcheck analysis..."
cppcheck --enable=all --suppress=missingIncludeSystem --suppress=unusedFunction \ cppcheck --enable=all --check-level=exhaustive --suppress=missingIncludeSystem \
--suppress=constParameter --quiet --std=c99 main.c || { --quiet --std=c23 main.c || {
print_warning "cppcheck found issues (see output above)" print_warning "cppcheck found issues (see output above)"
} }

View File

@ -1,17 +1,24 @@
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <dirent.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <dirent.h>
#include <sys/stat.h>
#include <strings.h> #include <strings.h>
#include <SDL2/SDL.h> #include <sys/stat.h>
#include <SDL2/SDL_image.h>
#define WINDOW_WIDTH 800 #define WINDOW_WIDTH 800
#define WINDOW_HEIGHT 600 #define WINDOW_HEIGHT 600
#define MAX_PATH_LEN 512 #define MAX_PATH_LEN 512
#define MAX_FILES 1000 #define MAX_FILES 1000
// Auto-navigation and rendering constants
#define AUTO_NAV_INTERVAL_MS 100
#define BACKGROUND_COLOR_R 32
#define BACKGROUND_COLOR_G 32
#define BACKGROUND_COLOR_B 32
#define BACKGROUND_COLOR_A 255
typedef struct { typedef struct {
char **files; char **files;
int count; int count;
@ -46,7 +53,6 @@ static int is_image_file(const char* filename);
static int init_file_list(FileList *list, const char *path); static int init_file_list(FileList *list, const char *path);
static void cleanup_file_list(FileList *list); static void cleanup_file_list(FileList *list);
static char *get_current_file_path(const FileList *list); static char *get_current_file_path(const FileList *list);
static int navigate_to_file(FileList* list, const char* target_filename);
static int load_current_image(ImageViewer *viewer); static int load_current_image(ImageViewer *viewer);
static int navigate_next_image(ImageViewer *viewer); static int navigate_next_image(ImageViewer *viewer);
static int navigate_prev_image(ImageViewer *viewer); static int navigate_prev_image(ImageViewer *viewer);
@ -63,7 +69,9 @@ static int safe_copy_memory(void* dest, size_t dest_size, const void* src, size_
return 0; // Source too large for destination return 0; // Source too large for destination
} }
memcpy(dest, src, src_len); // NOLINT(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling) memcpy(dest,
src,
src_len); // NOLINT(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling)
return 1; // Success return 1; // Success
} }
@ -88,7 +96,12 @@ static int safe_format_path(char* dest, size_t dest_size, const char* dir, const
return 0; // Invalid parameters return 0; // Invalid parameters
} }
int ret = snprintf(dest, dest_size, "%s/%s", dir, filename); // NOLINT(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling) int ret = snprintf(
dest,
dest_size,
"%s/%s",
dir,
filename); // NOLINT(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling)
if (ret < 0 || (size_t)ret >= dest_size) { if (ret < 0 || (size_t)ret >= dest_size) {
return 0; // Formatting failed or path too long return 0; // Formatting failed or path too long
} }
@ -151,7 +164,7 @@ static int init_viewer(ImageViewer* viewer) {
viewer->left_key_held = 0; viewer->left_key_held = 0;
viewer->right_key_held = 0; viewer->right_key_held = 0;
viewer->last_auto_nav_time = 0; viewer->last_auto_nav_time = 0;
viewer->auto_nav_interval = 100; viewer->auto_nav_interval = AUTO_NAV_INTERVAL_MS;
return 1; return 1;
} }
@ -207,7 +220,7 @@ static int load_image(ImageViewer* viewer, const char* filename) {
} }
static void render_image(ImageViewer *viewer) { static void render_image(ImageViewer *viewer) {
SDL_SetRenderDrawColor(viewer->renderer, 32, 32, 32, 255); SDL_SetRenderDrawColor(viewer->renderer, BACKGROUND_COLOR_R, BACKGROUND_COLOR_G, BACKGROUND_COLOR_B, BACKGROUND_COLOR_A);
SDL_RenderClear(viewer->renderer); SDL_RenderClear(viewer->renderer);
if (!viewer->texture) { if (!viewer->texture) {
@ -234,8 +247,10 @@ static void handle_zoom(ImageViewer* viewer, float zoom_delta, int mouse_x, int
float old_zoom = viewer->zoom_factor; float old_zoom = viewer->zoom_factor;
viewer->zoom_factor += zoom_delta; viewer->zoom_factor += zoom_delta;
if (viewer->zoom_factor < 0.1f) viewer->zoom_factor = 0.1f; if (viewer->zoom_factor < 0.1f)
if (viewer->zoom_factor > 10.0f) viewer->zoom_factor = 10.0f; viewer->zoom_factor = 0.1f;
if (viewer->zoom_factor > 10.0f)
viewer->zoom_factor = 10.0f;
float zoom_ratio = viewer->zoom_factor / old_zoom; float zoom_ratio = viewer->zoom_factor / old_zoom;
@ -245,8 +260,10 @@ static void handle_zoom(ImageViewer* viewer, float zoom_delta, int mouse_x, int
int center_x = window_w / 2; int center_x = window_w / 2;
int center_y = window_h / 2; int center_y = window_h / 2;
viewer->offset_x = (viewer->offset_x - (mouse_x - center_x)) * zoom_ratio + (mouse_x - center_x); viewer->offset_x =
viewer->offset_y = (viewer->offset_y - (mouse_y - center_y)) * zoom_ratio + (mouse_y - center_y); (viewer->offset_x - (mouse_x - center_x)) * zoom_ratio + (mouse_x - center_x);
viewer->offset_y =
(viewer->offset_y - (mouse_y - center_y)) * zoom_ratio + (mouse_y - center_y);
} }
static void print_help() { static void print_help() {
@ -279,7 +296,8 @@ static void cleanup_viewer(ImageViewer* viewer) {
static int is_image_file(const char *filename) { static int is_image_file(const char *filename) {
const char *ext = strrchr(filename, '.'); const char *ext = strrchr(filename, '.');
if (!ext) return 0; if (!ext)
return 0;
ext++; // Skip the dot ext++; // Skip the dot
return (strcasecmp(ext, "jpg") == 0 || strcasecmp(ext, "jpeg") == 0 || return (strcasecmp(ext, "jpg") == 0 || strcasecmp(ext, "jpeg") == 0 ||
@ -315,7 +333,7 @@ static int init_file_list(FileList* list, const char* path) {
} }
// First pass: count image files // First pass: count image files
struct dirent* entry; const struct dirent *entry;
while ((entry = readdir(dir)) != NULL) { while ((entry = readdir(dir)) != NULL) {
if (entry->d_name[0] != '.' && is_image_file(entry->d_name)) { if (entry->d_name[0] != '.' && is_image_file(entry->d_name)) {
// Build full path and check if it's a regular file // Build full path and check if it's a regular file
@ -359,7 +377,8 @@ static int init_file_list(FileList* list, const char* path) {
list->files[index] = malloc(strlen(entry->d_name) + 1); list->files[index] = malloc(strlen(entry->d_name) + 1);
if (list->files[index]) { if (list->files[index]) {
size_t name_len = strlen(entry->d_name); size_t name_len = strlen(entry->d_name);
if (safe_copy_string(list->files[index], name_len + 1, entry->d_name, name_len)) { if (safe_copy_string(
list->files[index], name_len + 1, entry->d_name, name_len)) {
index++; index++;
} else { } else {
free(list->files[index]); free(list->files[index]);
@ -388,8 +407,10 @@ static int init_file_list(FileList* list, const char* path) {
char *dot1 = strrchr(name1, '.'); char *dot1 = strrchr(name1, '.');
char *dot2 = strrchr(name2, '.'); char *dot2 = strrchr(name2, '.');
if (dot1) *dot1 = '\0'; if (dot1)
if (dot2) *dot2 = '\0'; *dot1 = '\0';
if (dot2)
*dot2 = '\0';
// Custom comparison: shorter names first, then alphabetical // Custom comparison: shorter names first, then alphabetical
int should_swap = 0; int should_swap = 0;
@ -448,13 +469,14 @@ static int init_file_list(FileList* list, const char* path) {
} }
// First pass: count image files in directory // First pass: count image files in directory
struct dirent* entry; const struct dirent *entry;
list->count = 0; list->count = 0;
while ((entry = readdir(dir)) != NULL) { while ((entry = readdir(dir)) != NULL) {
if (entry->d_name[0] != '.' && is_image_file(entry->d_name)) { if (entry->d_name[0] != '.' && is_image_file(entry->d_name)) {
// Build full path and check if it's a regular file // Build full path and check if it's a regular file
char full_path[MAX_PATH_LEN * 2]; char full_path[MAX_PATH_LEN * 2];
if (!safe_format_path(full_path, sizeof(full_path), list->base_dir, entry->d_name)) { if (!safe_format_path(
full_path, sizeof(full_path), list->base_dir, entry->d_name)) {
continue; // Skip if path formatting fails or path too long continue; // Skip if path formatting fails or path too long
} }
struct stat file_stat; struct stat file_stat;
@ -485,7 +507,8 @@ static int init_file_list(FileList* list, const char* path) {
if (entry->d_name[0] != '.' && is_image_file(entry->d_name)) { if (entry->d_name[0] != '.' && is_image_file(entry->d_name)) {
// Build full path and check if it's a regular file // Build full path and check if it's a regular file
char full_path[MAX_PATH_LEN * 2]; char full_path[MAX_PATH_LEN * 2];
if (!safe_format_path(full_path, sizeof(full_path), list->base_dir, entry->d_name)) { if (!safe_format_path(
full_path, sizeof(full_path), list->base_dir, entry->d_name)) {
continue; // Skip if path formatting fails or path too long continue; // Skip if path formatting fails or path too long
} }
struct stat file_stat; struct stat file_stat;
@ -493,7 +516,8 @@ static int init_file_list(FileList* list, const char* path) {
list->files[index] = malloc(strlen(entry->d_name) + 1); list->files[index] = malloc(strlen(entry->d_name) + 1);
if (list->files[index]) { if (list->files[index]) {
size_t name_len = strlen(entry->d_name); size_t name_len = strlen(entry->d_name);
if (safe_copy_string(list->files[index], name_len + 1, entry->d_name, name_len)) { if (safe_copy_string(
list->files[index], name_len + 1, entry->d_name, name_len)) {
index++; index++;
} else { } else {
free(list->files[index]); free(list->files[index]);
@ -522,8 +546,10 @@ static int init_file_list(FileList* list, const char* path) {
char *dot1 = strrchr(name1, '.'); char *dot1 = strrchr(name1, '.');
char *dot2 = strrchr(name2, '.'); char *dot2 = strrchr(name2, '.');
if (dot1) *dot1 = '\0'; if (dot1)
if (dot2) *dot2 = '\0'; *dot1 = '\0';
if (dot2)
*dot2 = '\0';
// Custom comparison: shorter names first, then alphabetical // Custom comparison: shorter names first, then alphabetical
int should_swap = 0; int should_swap = 0;
@ -554,7 +580,8 @@ static int init_file_list(FileList* list, const char* path) {
} }
} }
printf("Found %d image files in directory, starting with: %s\n", list->count, target_filename); printf(
"Found %d image files in directory, starting with: %s\n", list->count, target_filename);
} else { } else {
printf("Error: %s is neither a file nor a directory\n", path); printf("Error: %s is neither a file nor a directory\n", path);
@ -584,15 +611,15 @@ static char* get_current_file_path(const FileList* list) {
} }
static char full_path[MAX_PATH_LEN * 2]; static char full_path[MAX_PATH_LEN * 2];
if (!safe_format_path(full_path, sizeof(full_path), list->base_dir, list->files[list->current_index])) { if (!safe_format_path(
full_path, sizeof(full_path), list->base_dir, list->files[list->current_index])) {
return NULL; // Path formatting failed or path too long return NULL; // Path formatting failed or path too long
} }
return full_path; return full_path;
} }
static int load_current_image(ImageViewer *viewer) { static int load_current_image(ImageViewer *viewer) {
char* file_path = get_current_file_path(&viewer->file_list); const char *file_path = get_current_file_path(&viewer->file_list);
if (!file_path) { if (!file_path) {
printf("No current file to load\n"); printf("No current file to load\n");
return 0; return 0;
@ -602,16 +629,20 @@ static int load_current_image(ImageViewer* viewer) {
} }
static int navigate_next_image(ImageViewer *viewer) { static int navigate_next_image(ImageViewer *viewer) {
if (viewer->file_list.count <= 1) return 0; if (viewer->file_list.count <= 1)
return 0;
viewer->file_list.current_index = (viewer->file_list.current_index + 1) % viewer->file_list.count; viewer->file_list.current_index =
(viewer->file_list.current_index + 1) % viewer->file_list.count;
return load_current_image(viewer); return load_current_image(viewer);
} }
static int navigate_prev_image(ImageViewer *viewer) { static int navigate_prev_image(ImageViewer *viewer) {
if (viewer->file_list.count <= 1) return 0; if (viewer->file_list.count <= 1)
return 0;
viewer->file_list.current_index = (viewer->file_list.current_index - 1 + viewer->file_list.count) % viewer->file_list.count; viewer->file_list.current_index =
(viewer->file_list.current_index - 1 + viewer->file_list.count) % viewer->file_list.count;
return load_current_image(viewer); return load_current_image(viewer);
} }
@ -629,7 +660,6 @@ static void handle_auto_navigation(ImageViewer* viewer) {
if ((viewer->left_key_held || viewer->right_key_held) && if ((viewer->left_key_held || viewer->right_key_held) &&
(current_time - viewer->last_auto_nav_time >= viewer->auto_nav_interval)) { (current_time - viewer->last_auto_nav_time >= viewer->auto_nav_interval)) {
if (viewer->left_key_held) { if (viewer->left_key_held) {
if (navigate_prev_image(viewer)) { if (navigate_prev_image(viewer)) {
print_current_image_info(viewer); print_current_image_info(viewer);
@ -697,8 +727,7 @@ int main(int argc, char* argv[]) {
printf("Reset view\n"); printf("Reset view\n");
break; break;
case SDLK_f: case SDLK_f: {
{
int window_w, window_h; int window_w, window_h;
SDL_GetWindowSize(viewer.window, &window_w, &window_h); SDL_GetWindowSize(viewer.window, &window_w, &window_h);
@ -708,8 +737,7 @@ int main(int argc, char* argv[]) {
viewer.offset_x = 0; viewer.offset_x = 0;
viewer.offset_y = 0; viewer.offset_y = 0;
printf("Fit to window (zoom: %.2f)\n", viewer.zoom_factor); printf("Fit to window (zoom: %.2f)\n", viewer.zoom_factor);
} } break;
break;
case SDLK_PLUS: case SDLK_PLUS:
case SDLK_EQUALS: case SDLK_EQUALS:
@ -762,15 +790,13 @@ int main(int argc, char* argv[]) {
} }
break; break;
case SDL_MOUSEWHEEL: case SDL_MOUSEWHEEL: {
{
int mouse_x, mouse_y; int mouse_x, mouse_y;
SDL_GetMouseState(&mouse_x, &mouse_y); SDL_GetMouseState(&mouse_x, &mouse_y);
float zoom_delta = e.wheel.y * 0.1f; float zoom_delta = e.wheel.y * 0.1f;
handle_zoom(&viewer, zoom_delta, mouse_x, mouse_y); handle_zoom(&viewer, zoom_delta, mouse_x, mouse_y);
printf("Zoom: %.2f\n", viewer.zoom_factor); printf("Zoom: %.2f\n", viewer.zoom_factor);
} } break;
break;
case SDL_MOUSEBUTTONDOWN: case SDL_MOUSEBUTTONDOWN:
if (e.button.button == SDL_BUTTON_LEFT) { if (e.button.button == SDL_BUTTON_LEFT) {