commit a93c555dca8d5012b2cc7d48fa92cdb35d48bb81
parent ecf1f32724b5f3cf81a4f447aead0b04f6540d6a
Author: Friedel Schön <[email protected]>
Date: Mon, 15 May 2023 22:47:03 +0200
safer code + pedantic warnings
Diffstat:
9 files changed, 27 insertions(+), 29 deletions(-)
diff --git a/Makefile b/Makefile
@@ -8,9 +8,9 @@ MAN_DIR := src/man
ROFF_DIR := man
# Compiler Options
-CC ?= gcc
-CFLAGS ?= -g -std=gnu99 -pedantic
-LDFLAGS ?= -fPIE
+CC ?= clang
+CFLAGS += -g -std=gnu99 -Wpedantic
+LDFLAGS += -fPIE
# Executable-specific flags
finit_FLAGS := -static
diff --git a/include/util.h b/include/util.h
@@ -19,4 +19,4 @@ ssize_t writestr(int fd, const char* str);
unsigned int stat_mode(const char* format, ...);
-int fork_dup_cd_exec(int dir, const char* path, int fd0, int fd1, int fd2);
-\ No newline at end of file
+int fork_dup_cd_exec(int dir, const char* path, int fd0, int fd1, int fd2);
diff --git a/src/command_handler.c b/src/command_handler.c
@@ -124,7 +124,7 @@ int service_handle_command(void* argv, sv_command_t command, unsigned char extra
if (argv == NULL)
return -ENOSV;
- strcpy(runlevel, argv);
+ strncpy(runlevel, argv, SV_NAME_MAX);
if (extra == 1) {
for (int i = 0; i < services_size; i++) {
diff --git a/src/exec/finit.c b/src/exec/finit.c
@@ -32,8 +32,8 @@ int handle_initctl(int argc, const char** argv) {
}
-void handle_stage1();
-void handle_stage3();
+void handle_stage1(void);
+void handle_stage3(void);
static bool do_reboot;
diff --git a/src/exec/fsvc.c b/src/exec/fsvc.c
@@ -24,34 +24,34 @@ static const char VERSION_MESSAGE[] =
void print_status(service_t* s, char* state, size_t size) {
switch (s->state) {
case STATE_SETUP:
- strcpy(state, "setup");
+ strncpy(state, "setup", size);
break;
case STATE_INACTIVE:
- strcpy(state, "inactive");
+ strncpy(state, "inactive", size);
break;
case STATE_STARTING:
- strcpy(state, "starting");
+ strncpy(state, "starting", size);
break;
case STATE_ACTIVE_PID:
snprintf(state, size, "active (pid) as %d", s->pid);
break;
case STATE_ACTIVE_BACKGROUND:
- strcpy(state, "active (background)");
+ strncpy(state, "active (background)", size);
break;
case STATE_ACTIVE_DUMMY:
- strcpy(state, "active (dummy)");
+ strncpy(state, "active (dummy)", size);
break;
case STATE_ACTIVE_FOREGROUND:
snprintf(state, size, "active as %d", s->pid);
break;
case STATE_FINISHING:
- strcpy(state, "finishing");
+ strncpy(state, "finishing", size);
break;
case STATE_STOPPING:
- strcpy(state, "stopping");
+ strncpy(state, "stopping", size);
break;
case STATE_DEAD:
- strcpy(state, "dead");
+ strncpy(state, "dead", size);
break;
}
time_t diff = time(NULL) - s->status_change;
@@ -148,7 +148,7 @@ static const struct option long_options[] = {
};
int main(int argc, char** argv) {
- strcpy(runlevel, getenv(SV_RUNLEVEL_DEFAULT_ENV) ?: SV_RUNLEVEL_DEFAULT);
+ strncpy(runlevel, getenv(SV_RUNLEVEL_DEFAULT_ENV) ? getenv(SV_RUNLEVEL_DEFAULT_ENV) : SV_RUNLEVEL_DEFAULT, SV_NAME_MAX);
char* argexec = argv[0];
@@ -163,7 +163,7 @@ int main(int argc, char** argv) {
while ((c = getopt_long(argc, argv, ":Vvqr:pocf", long_options, NULL)) > 0) {
switch (c) {
case 'r':
- strcpy(runlevel, optarg);
+ strncpy(runlevel, optarg, SV_NAME_MAX);
break;
case 'q':
short_ = true;
@@ -320,4 +320,4 @@ int main(int argc, char** argv) {
print_service(&response[i], log);
}
}
-}
-\ No newline at end of file
+}
diff --git a/src/exec/zzz.c b/src/exec/zzz.c
@@ -115,4 +115,4 @@ int main(int argc, char** argv) {
wait(NULL);
}
-}
-\ No newline at end of file
+}
diff --git a/src/register.c b/src/register.c
@@ -34,7 +34,7 @@ service_t* service_register(int dir, const char* name, bool is_log_service) {
return NULL;
}
- strcpy(s->name, name);
+ strncpy(s->name, name, sizeof(s->name));
}
struct stat st;
diff --git a/src/supervise.c b/src/supervise.c
@@ -1,3 +1,4 @@
+#include "config.h"
#include "service.h"
#include "util.h"
@@ -44,7 +45,7 @@ static void signal_child(int unused) {
service_check_state(s, WIFSIGNALED(status), WIFSIGNALED(status) ? WTERMSIG(status) : WEXITSTATUS(status));
}
-static void check_deaths() {
+static void check_deaths(void) {
service_t* s;
for (int i = 0; i < services_size; i++) {
s = &services[i];
@@ -55,7 +56,7 @@ static void check_deaths() {
}
}
-static void check_services() {
+static void check_services(void) {
service_t* s;
for (int i = 0; i < services_size; i++) {
s = &services[i];
@@ -73,7 +74,7 @@ static void check_services() {
}
}
-static void accept_socket() {
+static void accept_socket(void) {
int client_fd;
if ((client_fd = accept(control_socket, NULL, NULL)) == -1) {
if (errno == EWOULDBLOCK) {
@@ -93,7 +94,7 @@ int service_supervise(const char* service_dir_, const char* runlevel_, bool forc
sigact.sa_handler = SIG_IGN;
sigaction(SIGPIPE, &sigact, NULL);
- strcpy(runlevel, runlevel_);
+ strncpy(runlevel, runlevel_, SV_NAME_MAX);
if ((service_dir = open(service_dir_, O_DIRECTORY)) == -1) {
print_error("error: cannot open directory %s: %s\n", service_dir_);
return 1;
@@ -134,7 +135,7 @@ int service_supervise(const char* service_dir_, const char* runlevel_, bool forc
// bind socket to address
struct sockaddr_un addr = { 0 };
addr.sun_family = AF_UNIX;
- strcpy(addr.sun_path, socket_path);
+ strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path));
if (bind(control_socket, (struct sockaddr*) &addr, sizeof(addr)) == -1) {
print_error("error: cannot bind %s to socket: %s\n", socket_path);
return 1;
diff --git a/src/user_group.c b/src/user_group.c
@@ -40,7 +40,7 @@ int parse_ugid(char* str, uid_t* uid, gid_t* gids) {
int gid_size = 0;
if (str[0] == ':')
- return (parse_ugid_num(str + 1, uid, gids));
+ return parse_ugid_num(str + 1, uid, gids);
if ((end = strchr(str, ':')) != NULL) {
end[0] = '\0';