fixups
Some checks failed
Release / Bump Homebrew formula (push) Has been cancelled

This commit is contained in:
2025-11-28 03:20:33 -08:00
parent 78e4f84f7b
commit 5d581c1c2f
2 changed files with 70 additions and 58 deletions

View File

@@ -4,7 +4,7 @@ project(ke C) # Specify C language explicitly
set(CMAKE_C_STANDARD 99) set(CMAKE_C_STANDARD 99)
set(KE_VERSION "2.0.1") set(KE_VERSION "2.0.1")
set(CMAKE_C_FLAGS "-Wall -Wextra -pedantic -Wshadow -Werror -std=c99 -g") set(CMAKE_C_FLAGS "-Wall -Wextra -pedantic -Wshadow -Werror -std=c99 -g -Werror=stringop-truncation")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_DEFAULT_SOURCE -D_XOPEN_SOURCE") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_DEFAULT_SOURCE -D_XOPEN_SOURCE")
# Optionally enable AddressSanitizer (ASan) # Optionally enable AddressSanitizer (ASan)

126
main.c
View File

@@ -298,8 +298,15 @@ file_open_prompt_cb(char *buf, const int16_t key)
strncat(newbuf, "/", sizeof(newbuf) - strlen(newbuf) - 1); strncat(newbuf, "/", sizeof(newbuf) - strlen(newbuf) - 1);
} }
strncpy(buf, newbuf[0] ? newbuf : names[0], 127); /* copy to input buffer (max 127 chars + NUL) avoiding truncation warnings */
buf[127] = '\0'; do {
const char *src__ = newbuf[0] ? newbuf : names[0];
size_t cap__ = 128u;
size_t len__ = strnlen(src__, cap__ - 1);
memcpy(buf, src__, len__);
buf[len__] = '\0';
} while (0);
editor_set_status("Unique match: %s%s", names[0], isdir[0] ? "/" : ""); editor_set_status("Unique match: %s%s", names[0], isdir[0] ? "/" : "");
} else { } else {
cur = strlen(base); cur = strlen(base);
@@ -318,8 +325,14 @@ file_open_prompt_cb(char *buf, const int16_t key)
strncat(newbuf, base, sizeof(newbuf) - strlen(newbuf) - 1); strncat(newbuf, base, sizeof(newbuf) - strlen(newbuf) - 1);
} }
strncpy(buf, newbuf, 127); /* copy to input buffer (max 127 chars + NUL) avoiding truncation warnings */
buf[127] = '\0'; do {
const char *src__ = newbuf;
size_t cap__ = 128u;
size_t len__ = strnlen(src__, cap__ - 1);
memcpy(buf, src__, len__);
buf[len__] = '\0';
} while (0);
size_t used = 0; size_t used = 0;
used += snprintf(msg + used, sizeof(msg) - used, "%d matches: ", n); used += snprintf(msg + used, sizeof(msg) - used, "%d matches: ", n);
@@ -842,37 +855,36 @@ delete_region(void)
void void
jump_to_position(int col, int row) jump_to_position(int col, int row)
{ {
/* Handle empty buffer safely */ if (ENROWS <= 0) {
if (ENROWS <= 0) { ECURX = 0;
ECURX = 0; ECURY = 0;
ECURY = 0;
/* Keep legacy globals in sync with per-buffer fields for rendering */
editor.curx = ECURX;
editor.cury = ECURY;
display_refresh();
return;
}
/* clamp position using per-buffer dimensions */ editor.curx = ECURX;
if (row < 0) { editor.cury = ECURY;
row = 0;
} else if (row >= ENROWS) {
row = ENROWS - 1;
}
if (col < 0) { display_refresh();
col = 0; return;
} else if (col > (int) EROW[row].size) { }
col = (int) EROW[row].size;
}
ECURX = col; if (row < 0) {
ECURY = row; row = 0;
/* Keep legacy globals in sync with per-buffer fields for rendering */ } else if (row >= ENROWS) {
editor.curx = ECURX; row = ENROWS - 1;
editor.cury = ECURY; }
display_refresh(); if (col < 0) {
col = 0;
} else if (col > (int) EROW[row].size) {
col = (int) EROW[row].size;
}
ECURX = col;
ECURY = row;
editor.curx = ECURX;
editor.cury = ECURY;
display_refresh();
} }
@@ -886,13 +898,13 @@ goto_line(void)
return; return;
} }
lineno = atoi(query); lineno = atoi(query);
if (lineno < 1 || lineno > ENROWS) { if (lineno < 1 || lineno > ENROWS) {
editor_set_status("Line number must be between 1 and %d.", editor_set_status("Line number must be between 1 and %d.",
ENROWS); ENROWS);
free(query); free(query);
return; return;
} }
jump_to_position(0, lineno - 1); jump_to_position(0, lineno - 1);
free(query); free(query);
@@ -1698,27 +1710,27 @@ editor_find(void)
void void
editor_openfile(void) editor_openfile(void)
{ {
char *filename; char *filename = NULL;
buffer *cur = NULL;
int nb = NULL;
/* Add TAB completion for path input */ filename = editor_prompt("Load file: %s", file_open_prompt_cb);
filename = editor_prompt("Load file: %s", file_open_prompt_cb); if (filename == NULL) {
if (filename == NULL) { return;
return; }
}
/* If the only buffer is an unnamed, empty buffer, reuse it */ cur = buffer_current();
buffer *cur = buffer_current(); if (editor.bufcount == 1 && buffer_is_unnamed_and_empty(cur)) {
if (editor.bufcount == 1 && buffer_is_unnamed_and_empty(cur)) { open_file(filename);
open_file(filename); buffer_save_current();
buffer_save_current(); } else {
} else { nb = buffer_add_empty();
/* Open into a new buffer */ buffer_switch(nb);
int nb = buffer_add_empty(); open_file(filename);
buffer_switch(nb); buffer_save_current();
open_file(filename); }
buffer_save_current();
} free(filename);
free(filename);
} }