From 01ff62c0e497ab498351e1e1a1d8088aeb2130e3 Mon Sep 17 00:00:00 2001 From: hybrid Date: Tue, 2 Jun 2026 13:05:35 +0300 Subject: fix: malloc and space in tmp string --- Makefile | 2 +- src/main.c | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index f07c32b..d9c25dc 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ CC ?= gcc CFLAGS := -Wall -O2 -std=c99 LDFLAGS := -LDLIBS := +LDLIBS := -lm SRCDIR := src BINDIR := bin diff --git a/src/main.c b/src/main.c index 50beaaa..3f94e3b 100644 --- a/src/main.c +++ b/src/main.c @@ -1,6 +1,7 @@ #include #include #include +#include #include #include @@ -25,17 +26,17 @@ int main(int argc, char **argv) { for (int i = 1; i < argc; i++) { char *tmp = NULL; - if ((tmp = malloc(strlen(argv[i]) + 4)) == NULL) { + if ((tmp = malloc(strlen(argv[i]) + sizeof(char) * (int)log10(i))) + == NULL) { perror("main: malloc failed"); exit(EXIT_FAILURE); } - sprintf(tmp, "[%d] %s ", i, argv[i]); + sprintf(tmp, " [%d] %s", i, argv[i]); strcat(str, tmp); free(tmp); } - str[strlen(str)] = '\0'; syslog(LOG_DEBUG, "args: %s\n", str); -- cgit v1.3.1