Skip to content

Commit 4d5f622

Browse files
committed
Char arrays instead of pointers in path functions
Signed-off-by: Anna (navi) Figueiredo Gomes <navi@vlhl.dev>
1 parent b3e77e4 commit 4d5f622

File tree

5 files changed

+84
-104
lines changed

5 files changed

+84
-104
lines changed

src/librc/librc-depend.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,13 @@
3737

3838
#define RC_DEPCONFIG "/depconfig"
3939

40-
static char *configpath = NULL;
4140
static char*
42-
depconfigpath_get() {
43-
if (!configpath) {
44-
configpath = xmalloc(sizeof(char) * PATH_MAX);
45-
snprintf(configpath, PATH_MAX, "%s/%s", rc_svcdir_get(), RC_DEPCONFIG);
41+
depconfigpath_get(void) {
42+
static char path[PATH_MAX] = { 0 };
43+
if (!*path) {
44+
snprintf(path, PATH_MAX, "%s/%s", rc_svcdir_get(), RC_DEPCONFIG);
4645
}
47-
return configpath;
46+
return path;
4847
}
4948

5049
static const char *bootlevel = NULL;

src/librc/librc.c

Lines changed: 30 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,6 @@
4848
/* File stream used for plugins to write environ vars to */
4949
FILE *rc_environ_fd = NULL;
5050

51-
static char* rc_svcdir = NULL;
52-
static char* rc_sysconfdir = NULL;
53-
static char* rc_initdir = NULL;
54-
static char* rc_confdir = NULL;
55-
static char* rc_runleveldir = NULL;
56-
5751
typedef struct rc_service_state_name {
5852
RC_SERVICE state;
5953
const char *name;
@@ -375,71 +369,71 @@ rc_sys(void)
375369

376370
char *
377371
rc_sysconfdir_get() {
372+
static char path[PATH_MAX] = { 0 };
378373
if (geteuid() != 0) {
379-
if (!rc_sysconfdir) {
380-
char *path;
381-
rc_sysconfdir = xmalloc(sizeof(char) * PATH_MAX);
382-
if ((path = getenv("XDG_CONFIG_HOME"))) {
383-
snprintf(rc_sysconfdir, PATH_MAX, "%s", path);
384-
} else if ((path = getenv("HOME"))) {
385-
snprintf(rc_sysconfdir, PATH_MAX, "%s/%s", path, ".config");
374+
if (!*path) {
375+
char *config_path;
376+
if ((config_path = getenv("XDG_CONFIG_HOME"))) {
377+
snprintf(path, PATH_MAX, "%s", config_path);
378+
} else if ((config_path = getenv("HOME"))) {
379+
snprintf(path, PATH_MAX, "%s/%s", config_path, ".config");
386380
} else {
387381
exit(1);
388382
}
389383
}
390-
return rc_sysconfdir;
384+
return path;
391385
} else {
392386
return RC_SYSCONFDIR;
393387
}
394388
}
395389

396390
char *
397391
rc_svcdir_get() {
392+
static char path[PATH_MAX] = { 0 };
398393
if (geteuid() != 0) {
399-
if (!rc_svcdir) {
400-
char *path;
401-
rc_svcdir = xmalloc(sizeof(char) * PATH_MAX);
402-
if ((path = getenv("XDG_RUNTIME_DIR")) ||
403-
(path = getenv("XDG_DATA_HOME"))) {
404-
snprintf(rc_svcdir, PATH_MAX, "%s/%s", path, "openrc");
405-
} else if ((path = getenv("HOME"))) {
406-
snprintf(rc_svcdir, PATH_MAX, "%s/%s/%s",
407-
path, ".local/share", "openrc");
394+
if (!*path) {
395+
char *config_path;
396+
if ((config_path = getenv("XDG_RUNTIME_DIR")) ||
397+
(config_path = getenv("XDG_DATA_HOME"))) {
398+
snprintf(path, PATH_MAX, "%s/%s", config_path, "openrc");
399+
} else if ((config_path = getenv("HOME"))) {
400+
snprintf(path, PATH_MAX, "%s/%s/%s",
401+
config_path, ".local/share", "openrc");
408402
} else {
409403
exit(1);
410404
}
411405
}
412-
return rc_svcdir;
406+
return path;
413407
} else {
414408
return RC_SVCDIR;
415409
}
416410
}
417411

418412
char *
419413
rc_initdir_get() {
420-
if (!rc_initdir) {
421-
rc_initdir = xmalloc(sizeof(char) * PATH_MAX);
422-
snprintf(rc_initdir, PATH_MAX, "%s%s", rc_sysconfdir_get(), RC_INITDIR);
414+
static char path[PATH_MAX] = { 0 };
415+
if (!*path) {
416+
snprintf(path, PATH_MAX, "%s%s", rc_sysconfdir_get(), RC_INITDIR);
423417
}
424-
return rc_initdir;
418+
return path;
425419
}
426420

427421
char *
428422
rc_confdir_get() {
429-
if (!rc_confdir) {
430-
rc_confdir = xmalloc(sizeof(char) * PATH_MAX);
431-
snprintf(rc_confdir, PATH_MAX, "%s%s", rc_sysconfdir_get(), RC_CONFDIR);
423+
static char path[PATH_MAX] = { 0 };
424+
if (!*path) {
425+
snprintf(path, PATH_MAX, "%s%s", rc_sysconfdir_get(), RC_CONFDIR);
432426
}
433-
return rc_confdir;
427+
return path;
434428
}
435429

436430
char *
437431
rc_runleveldir_get() {
438-
if (!rc_runleveldir) {
439-
rc_runleveldir = xmalloc(sizeof(char) * PATH_MAX);
440-
snprintf(rc_runleveldir, PATH_MAX, "%s%s", rc_sysconfdir_get(), RC_RUNLEVELDIR);
432+
static char path[PATH_MAX] = { 0 };
433+
if (!*path) {
434+
snprintf(path, PATH_MAX, "%s%s", rc_sysconfdir_get(), RC_RUNLEVELDIR);
441435
}
442-
return rc_runleveldir;
436+
return path;
443437
}
444438

445439
static const char *

src/openrc-run/openrc-run.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <fnmatch.h>
2222
#include <getopt.h>
2323
#include <libgen.h>
24+
#include <limits.h>
2425
#include <poll.h>
2526
#include <signal.h>
2627
#include <stdio.h>
@@ -56,15 +57,13 @@
5657

5758
#define PREFIX_LOCK "prefix.lock"
5859

59-
static char *prefix_lock = NULL;
60-
6160
static char *
62-
prefix_lock_get() {
63-
if (!prefix_lock) {
64-
prefix_lock = xmalloc(sizeof(char) * PATH_MAX);
65-
snprintf(prefix_lock, PATH_MAX, "%s/%s", rc_svcdir_get(), PREFIX_LOCK);
61+
prefix_lock_get(void) {
62+
static char path[PATH_MAX] = { 0 };
63+
if (!path[0]) {
64+
snprintf(path, PATH_MAX, "%s/%s", rc_svcdir_get(), PREFIX_LOCK);
6665
}
67-
return prefix_lock;
66+
return path;
6867
}
6968

7069
#define WAIT_INTERVAL 20000000 /* usecs to poll the lock file */
@@ -359,7 +358,7 @@ svc_exec(const char *arg1, const char *arg2)
359358
int slave_tty;
360359
sigset_t sigchldmask;
361360
sigset_t oldmask;
362-
char openrcshpath[sizeof(char) * PATH_MAX];
361+
char openrcshpath[PATH_MAX];
363362

364363
/* Setup our signal pipe */
365364
if (pipe(signal_pipe) == -1)

src/openrc/rc-logger.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,16 @@
4747
#include "helpers.h"
4848

4949
#define TMPLOG "rc.log"
50-
static char *tmplog = NULL;
5150

5251
static char *
53-
gettmplog() {
54-
if (!tmplog) {
55-
tmplog = xmalloc(sizeof(char) * PATH_MAX);
56-
snprintf(tmplog, PATH_MAX, "%s/%s", rc_svcdir_get(), TMPLOG);
52+
gettmplog(void) {
53+
static char path[PATH_MAX] = { 0 };
54+
if (!path[0]) {
55+
snprintf(path, PATH_MAX, "%s/%s", rc_svcdir_get(), TMPLOG);
5756
}
58-
return tmplog;
57+
return path;
5958
}
59+
6060
#define DEFAULTLOG "/var/log/rc.log"
6161

6262
static int signal_pipe[2] = { -1, -1 };

src/shared/misc.c

Lines changed: 36 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -43,104 +43,92 @@
4343

4444
extern char **environ;
4545

46-
47-
static char *deptree_cache_path = NULL;
48-
static char *deptree_skewed_path = NULL;
49-
static char *krunlevel_path = NULL;
50-
static char *starting_path = NULL;
51-
static char *stopping_path = NULL;
52-
53-
static char *svcdir_starting = NULL;
54-
static char *svcdir_inactive = NULL;
55-
static char *svcdir_started = NULL;
56-
static char *svcdir_coldplugged = NULL;
57-
5846
char *
5947
rc_deptree_cache_path_get() {
60-
if (!deptree_cache_path) {
61-
deptree_cache_path = xmalloc(sizeof(char) * PATH_MAX);
62-
snprintf(deptree_cache_path, PATH_MAX, "%s/%s",
48+
static char path[PATH_MAX] = { 0 };
49+
if (!*path) {
50+
snprintf(path, PATH_MAX, "%s/%s",
6351
rc_svcdir_get(), RC_DEPTREE_CACHE);
6452
}
65-
return deptree_cache_path;
53+
return path;
6654
}
6755

6856
char *
6957
rc_deptree_skewed_path_get() {
70-
if (!deptree_skewed_path) {
71-
deptree_skewed_path = xmalloc(sizeof(char) * PATH_MAX);
72-
snprintf(deptree_skewed_path, PATH_MAX, "%s/%s",
58+
static char path[PATH_MAX] = { 0 };
59+
if (!*path) {
60+
snprintf(path, PATH_MAX, "%s/%s",
7361
rc_svcdir_get(), RC_DEPTREE_SKEWED);
7462
}
75-
return deptree_skewed_path;
63+
return path;
7664
}
7765

7866
char *
7967
rc_krunlevel_path_get() {
80-
if (!krunlevel_path) {
81-
krunlevel_path = xmalloc(sizeof(char) * PATH_MAX);
82-
snprintf(krunlevel_path, PATH_MAX, "%s/%s",
68+
static char path[PATH_MAX] = { 0 };
69+
if (!*path) {
70+
snprintf(path, PATH_MAX, "%s/%s",
8371
rc_svcdir_get(), RC_KRUNLEVEL);
8472
}
85-
return krunlevel_path;
73+
return path;
8674
}
8775
char *
8876
rc_starting_path_get() {
89-
if (!starting_path) {
90-
starting_path = xmalloc(sizeof(char) * PATH_MAX);
91-
snprintf(starting_path, PATH_MAX, "%s/%s",
77+
static char path[PATH_MAX] = { 0 };
78+
if (!*path) {
79+
snprintf(path, PATH_MAX, "%s/%s",
9280
rc_svcdir_get(), RC_STARTING);
9381
}
94-
return starting_path;
82+
return path;
9583
}
9684
char *
9785
rc_stopping_path_get() {
98-
if (!stopping_path) {
99-
stopping_path = xmalloc(sizeof(char) * PATH_MAX);
100-
snprintf(stopping_path, PATH_MAX, "%s/%s",
86+
static char path[PATH_MAX] = { 0 };
87+
if (!*path) {
88+
snprintf(path, PATH_MAX, "%s/%s",
10189
rc_svcdir_get(), RC_STOPPING);
10290
}
103-
return stopping_path;
91+
return path;
10492
}
10593

10694
char *
10795
rc_svcdir_starting_get() {
108-
if (!svcdir_starting) {
109-
svcdir_starting = xmalloc(sizeof(char) * PATH_MAX);
110-
snprintf(svcdir_starting, PATH_MAX, "%s/%s",
96+
static char path[PATH_MAX] = { 0 };
97+
if (!*path) {
98+
snprintf(path, PATH_MAX, "%s/%s",
11199
rc_svcdir_get(), RC_SVCDIR_STARTING);
112100
}
113-
return svcdir_starting;
101+
return path;
114102
}
115103

116104
char *
117105
rc_svcdir_inactive_get() {
118-
if (!svcdir_inactive) {
119-
svcdir_inactive = xmalloc(sizeof(char) * PATH_MAX);
120-
snprintf(svcdir_inactive, PATH_MAX, "%s/%s",
106+
static char path[PATH_MAX] = { 0 };
107+
if (!*path) {
108+
snprintf(path, PATH_MAX, "%s/%s",
121109
rc_svcdir_get(), RC_SVCDIR_INACTIVE);
122110
}
123-
return svcdir_inactive;
111+
return path;
124112
}
125113

126114
char *
127115
rc_svcdir_started_get() {
128-
if (!svcdir_started) {
129-
svcdir_started = xmalloc(sizeof(char) * PATH_MAX);
130-
snprintf(svcdir_started, PATH_MAX, "%s/%s",
116+
static char path[PATH_MAX] = { 0 };
117+
if (!*path) {
118+
snprintf(path, PATH_MAX, "%s/%s",
131119
rc_svcdir_get(), RC_SVCDIR_STARTED);
132120
}
133-
return svcdir_started;
121+
return path;
134122
}
135123

136124
char *
137125
rc_svcdir_coldplugged_get() {
138-
if (!svcdir_coldplugged) {
139-
svcdir_coldplugged = xmalloc(sizeof(char) * PATH_MAX);
140-
snprintf(svcdir_coldplugged, PATH_MAX, "%s/%s",
126+
static char path[PATH_MAX] = { 0 };
127+
if (!*path) {
128+
snprintf(path, PATH_MAX, "%s/%s",
141129
rc_svcdir_get(), RC_SVCDIR_COLDPLUGGED);
142130
}
143-
return svcdir_coldplugged;
131+
return path;
144132
}
145133

146134
bool

0 commit comments

Comments
 (0)