чување db20cfc29f809131216f83d2ae59c0c429602985
родитељ f029ec3f42a9263bc796fb067af266b936495324
Аутор: Страхиња Радић <sr@strahinja.org>
Датум: Sun, 25 Jan 2026 18:31:13 +0100
Fix SEGV when no filename and -b/-B; get rid of exit code 1
Diffstat:
измењених датотека: 4, додавања: 19(+), брисања: 7(-)
diff --git a/draw.c b/draw.c
@@ -60,6 +60,7 @@ static const char* mode_labels[] = {
[SELECTION_MODE] = "SELECTION"
};
const char* errors[] = {
+ [ERR_NO_CL_ARG] = "No filename given",
[ERR_BINARY] = "Not opening a binary file",
[ERR_NOTREG_FILE] = "'%s' is not a regular file",
[ERR_TB_ERROR] = "termbox error",
diff --git a/sled.1.in b/sled.1.in
@@ -205,12 +205,14 @@ that value is returned as the exit status.
Otherwise, the exit status is one of the following:
.Bl -tag -width Ds -offset indent -compact
.It Li 201
+No filename was given on the command line.
+.It Li 202
File is a binary file and
.Dv open_binary
is set to zero.
-.It Li 202
-File is not a regular file.
.It Li 203
+File is not a regular file.
+.It Li 204
There has been a termbox error.
.El
.
diff --git a/sled.c b/sled.c
@@ -2885,9 +2885,14 @@ main(int argc, char** argv)
setlocale(LC_ALL, "");
if (argc == 1)
- return print_error(1, "No filename given");
+ return print_error(CUSTOM_ERROR_START + ERR_NO_CL_ARG,
+ "No filename given");
main_options_loop:
+ if (optind == argc)
+ return print_error(CUSTOM_ERROR_START + ERR_NO_CL_ARG,
+ "No filename given");
+
if (!strcmp(argv[optind], "-h") || !strcmp(argv[optind], "--help"))
{
printf("Usage:\t%s -h | --help | -V | --full-version | "
@@ -2941,7 +2946,10 @@ main_options_loop:
/* argv[1] is the filename; BTW, "filename" is a misnomer */
filename_size = strlen(argv[optind]) + 1;
if (!(dir = malloc(filename_size)))
- return print_error(1, "Allocation failed (out of memory?)");
+ {
+ perror(PROGRAM_NAME ": malloc");
+ exit(errno);
+ }
(void)dirname(dir, argv[optind], filename_size);
if (init_limits(dir) < 0)
@@ -2954,8 +2962,8 @@ main_options_loop:
filename = calloc(limits[L_PATH_MAX], 1);
if (!filename)
{
- free(dir);
- return print_error(1, "Allocation failed (out of memory?)");
+ perror(PROGRAM_NAME ": calloc");
+ exit(errno);
}
*filename = 0;
@@ -3049,7 +3057,7 @@ main_options_loop:
limits[L_PATH_MAX], temp);
if (!temp)
{
- print_error(1, "Pathname too long");
+ errno = ENAMETOOLONG;
goto main_done;
}
*(temp - 1) = '/'; /* overwrite NUL */
diff --git a/util.h b/util.h
@@ -100,6 +100,7 @@
enum {
ERR_NONE,
+ ERR_NO_CL_ARG,
ERR_BINARY,
ERR_NOTREG_FILE,
ERR_TB_ERROR,