malloc over fatty buffers

This commit is contained in:
Roy Marples 2007-10-12 00:01:33 +00:00
parent e3bc6666d6
commit 20a149b2c7
10 changed files with 95 additions and 69 deletions

View File

@ -241,7 +241,7 @@ static bool _match_daemon (const char *path, const char *file,
const char *mexec, const char *mname, const char *mexec, const char *mname,
const char *mpidfile) const char *mpidfile)
{ {
char buffer[RC_LINEBUFFER]; char *buffer;
char *ffile = rc_strcatpaths (path, file, (char *) NULL); char *ffile = rc_strcatpaths (path, file, (char *) NULL);
FILE *fp; FILE *fp;
int lc = 0; int lc = 0;
@ -257,7 +257,8 @@ static bool _match_daemon (const char *path, const char *file,
if (! mpidfile) if (! mpidfile)
m += 100; m += 100;
memset (buffer, 0, sizeof (buffer)); buffer = xmalloc (sizeof (char) * RC_LINEBUFFER);
memset (buffer, 0, RC_LINEBUFFER);
while ((fgets (buffer, RC_LINEBUFFER, fp))) { while ((fgets (buffer, RC_LINEBUFFER, fp))) {
int lb = strlen (buffer) - 1; int lb = strlen (buffer) - 1;
if (buffer[lb] == '\n') if (buffer[lb] == '\n')
@ -277,6 +278,7 @@ static bool _match_daemon (const char *path, const char *file,
if (lc > 5) if (lc > 5)
break; break;
} }
free (buffer);
fclose (fp); fclose (fp);
free (ffile); free (ffile);
@ -439,7 +441,7 @@ bool rc_service_daemons_crashed (const char *service)
struct dirent *d; struct dirent *d;
char *path; char *path;
FILE *fp; FILE *fp;
char buffer[RC_LINEBUFFER]; char *buffer = NULL;
char *exec = NULL; char *exec = NULL;
char *name = NULL; char *name = NULL;
char *pidfile = NULL; char *pidfile = NULL;
@ -458,10 +460,13 @@ bool rc_service_daemons_crashed (const char *service)
(char *) NULL); (char *) NULL);
free (svc); free (svc);
if (! (dp = opendir (dirpath))) if (! (dp = opendir (dirpath))) {
free (dirpath);
return (false); return (false);
}
memset (buffer, 0, sizeof (buffer)); buffer = xmalloc (sizeof (char) * RC_LINEBUFFER);
memset (buffer, 0, RC_LINEBUFFER);
while ((d = readdir (dp))) { while ((d = readdir (dp))) {
if (d->d_name[0] == '.') if (d->d_name[0] == '.')
@ -542,6 +547,7 @@ bool rc_service_daemons_crashed (const char *service)
name = NULL; name = NULL;
} }
free (buffer);
free (exec); free (exec);
free (name); free (name);
free (dirpath); free (dirpath);

View File

@ -667,7 +667,7 @@ bool rc_deptree_update (void)
rc_deptype_t *deptype = NULL; rc_deptype_t *deptype = NULL;
rc_deptype_t *dt; rc_deptype_t *dt;
rc_deptype_t *last_deptype = NULL; rc_deptype_t *last_deptype = NULL;
char buffer[RC_LINEBUFFER]; char *buffer = NULL;
int len; int len;
int i; int i;
int j; int j;
@ -685,6 +685,7 @@ bool rc_deptree_update (void)
deptree = xmalloc (sizeof (rc_depinfo_t)); deptree = xmalloc (sizeof (rc_depinfo_t));
memset (deptree, 0, sizeof (rc_depinfo_t)); memset (deptree, 0, sizeof (rc_depinfo_t));
buffer = xmalloc (sizeof (char) * RC_LINEBUFFER);
memset (buffer, 0, RC_LINEBUFFER); memset (buffer, 0, RC_LINEBUFFER);
/* Phase 2 */ /* Phase 2 */
@ -776,6 +777,7 @@ bool rc_deptree_update (void)
} }
pclose (fp); pclose (fp);
free (buffer);
/* Phase 3 - add our providors to the tree */ /* Phase 3 - add our providors to the tree */
for (depinfo = deptree; depinfo; depinfo = depinfo->next) for (depinfo = deptree; depinfo; depinfo = depinfo->next)

View File

@ -90,7 +90,7 @@ char **rc_config_load (const char *file)
{ {
char **list = NULL; char **list = NULL;
FILE *fp; FILE *fp;
char buffer[RC_LINEBUFFER]; char *buffer;
char *p; char *p;
char *token; char *token;
char *line; char *line;
@ -104,6 +104,7 @@ char **rc_config_load (const char *file)
if (! (fp = fopen (file, "r"))) if (! (fp = fopen (file, "r")))
return (NULL); return (NULL);
buffer = xmalloc (sizeof (char) * RC_LINEBUFFER);
while (fgets (buffer, RC_LINEBUFFER, fp)) { while (fgets (buffer, RC_LINEBUFFER, fp)) {
p = buffer; p = buffer;
@ -164,6 +165,7 @@ char **rc_config_load (const char *file)
} }
free (entry); free (entry);
} }
free (buffer);
fclose (fp); fclose (fp);
return (list); return (list);
@ -189,7 +191,7 @@ librc_hidden_def(rc_config_value)
char **rc_config_list (const char *file) char **rc_config_list (const char *file)
{ {
FILE *fp; FILE *fp;
char buffer[RC_LINEBUFFER]; char *buffer;
char *p; char *p;
char *token; char *token;
char **list = NULL; char **list = NULL;
@ -197,6 +199,7 @@ char **rc_config_list (const char *file)
if (! (fp = fopen (file, "r"))) if (! (fp = fopen (file, "r")))
return (NULL); return (NULL);
buffer = xmalloc (sizeof (char) * RC_LINEBUFFER);
while (fgets (buffer, RC_LINEBUFFER, fp)) { while (fgets (buffer, RC_LINEBUFFER, fp)) {
p = buffer; p = buffer;
@ -214,6 +217,7 @@ char **rc_config_list (const char *file)
rc_strlist_add (&list, token); rc_strlist_add (&list, token);
} }
} }
free (buffer);
fclose (fp); fclose (fp);
return (list); return (list);

View File

@ -157,21 +157,23 @@ librc_hidden_def(rc_runlevel_list)
char *rc_runlevel_get (void) char *rc_runlevel_get (void)
{ {
FILE *fp; FILE *fp;
char buffer[RC_LINEBUFFER];
char *runlevel = NULL; char *runlevel = NULL;
if ((fp = fopen (SOFTLEVEL, "r"))) { if ((fp = fopen (SOFTLEVEL, "r"))) {
if (fgets (buffer, PATH_MAX, fp)) { runlevel = xmalloc (sizeof (char) * PATH_MAX);
int i = strlen (buffer) - 1; if (fgets (runlevel, PATH_MAX, fp)) {
if (buffer[i] == '\n') int i = strlen (runlevel) - 1;
buffer[i] = 0; if (runlevel[i] == '\n')
runlevel = xstrdup (buffer); runlevel[i] = 0;
} } else
*runlevel = '\0';
fclose (fp); fclose (fp);
} }
if (! runlevel) if (! runlevel || ! *runlevel) {
free (runlevel);
runlevel = xstrdup (RC_LEVEL_SYSINIT); runlevel = xstrdup (RC_LEVEL_SYSINIT);
}
return (runlevel); return (runlevel);
} }
@ -272,8 +274,8 @@ librc_hidden_def(rc_service_exists)
char **rc_service_options (const char *service) char **rc_service_options (const char *service)
{ {
char *svc; char *svc;
char cmd[PATH_MAX]; char *cmd = NULL;
char buffer[RC_LINEBUFFER]; char *buffer = NULL;
char **opts = NULL; char **opts = NULL;
char *token; char *token;
char *p = buffer; char *p = buffer;
@ -282,11 +284,10 @@ char **rc_service_options (const char *service)
if (! (svc = rc_service_resolve (service))) if (! (svc = rc_service_resolve (service)))
return (NULL); return (NULL);
snprintf (cmd, sizeof (cmd), ". '%s'; echo \"${opts}\"", svc); asprintf (&cmd, ". '%s'; echo \"${opts}\"", svc);
free (svc); free (svc);
if (! (fp = popen (cmd, "r"))) if ((fp = popen (cmd, "r"))) {
return (NULL); buffer = xmalloc (sizeof (char) * RC_LINEBUFFER);
if (fgets (buffer, RC_LINEBUFFER, fp)) { if (fgets (buffer, RC_LINEBUFFER, fp)) {
if (buffer[strlen (buffer) - 1] == '\n') if (buffer[strlen (buffer) - 1] == '\n')
buffer[strlen (buffer) - 1] = '\0'; buffer[strlen (buffer) - 1] = '\0';
@ -294,6 +295,8 @@ char **rc_service_options (const char *service)
rc_strlist_addsort (&opts, token); rc_strlist_addsort (&opts, token);
} }
pclose (fp); pclose (fp);
free (buffer);
}
return (opts); return (opts);
} }
librc_hidden_def(rc_service_options) librc_hidden_def(rc_service_options)
@ -301,8 +304,8 @@ librc_hidden_def(rc_service_options)
char *rc_service_description (const char *service, const char *option) char *rc_service_description (const char *service, const char *option)
{ {
char *svc; char *svc;
char cmd[PATH_MAX]; char *cmd = NULL;
char buffer[RC_LINEBUFFER]; char *buffer;
char *desc = NULL; char *desc = NULL;
FILE *fp; FILE *fp;
int i; int i;
@ -313,12 +316,11 @@ char *rc_service_description (const char *service, const char *option)
if (! option) if (! option)
option = ""; option = "";
snprintf (cmd, sizeof (cmd), ". '%s'; echo \"${description%s%s}\"", asprintf (&cmd, ". '%s'; echo \"${description%s%s}\"",
svc, option ? "_" : "", option); svc, option ? "_" : "", option);
free (svc); free (svc);
if (! (fp = popen (cmd, "r"))) if ((fp = popen (cmd, "r"))) {
return (NULL); buffer = xmalloc (sizeof (char) * RC_LINEBUFFER);
while (fgets (buffer, RC_LINEBUFFER, fp)) { while (fgets (buffer, RC_LINEBUFFER, fp)) {
if (! desc) { if (! desc) {
desc = xmalloc (strlen (buffer) + 1); desc = xmalloc (strlen (buffer) + 1);
@ -330,8 +332,9 @@ char *rc_service_description (const char *service, const char *option)
memcpy (desc + i, buffer, strlen (buffer)); memcpy (desc + i, buffer, strlen (buffer));
memset (desc + i + strlen (buffer), 0, 1); memset (desc + i + strlen (buffer), 0, 1);
} }
free (buffer);
pclose (fp); pclose (fp);
}
return (desc); return (desc);
} }
librc_hidden_def(rc_service_description) librc_hidden_def(rc_service_description)
@ -518,20 +521,18 @@ librc_hidden_def(rc_service_state)
char *rc_service_value_get (const char *service, const char *option) char *rc_service_value_get (const char *service, const char *option)
{ {
FILE *fp; FILE *fp;
char buffer[RC_LINEBUFFER]; char *buffer = NULL;
char *file = rc_strcatpaths (RC_SVCDIR, "options", service, option, char *file = rc_strcatpaths (RC_SVCDIR, "options", service, option,
(char *) NULL); (char *) NULL);
char *value = NULL;
if ((fp = fopen (file, "r"))) { if ((fp = fopen (file, "r"))) {
memset (buffer, 0, sizeof (buffer)); buffer = xmalloc (sizeof (char) * RC_LINEBUFFER);
if (fgets (buffer, RC_LINEBUFFER, fp)) fgets (buffer, RC_LINEBUFFER, fp);
value = xstrdup (buffer);
fclose (fp); fclose (fp);
} }
free (file); free (file);
return (value); return (buffer);
} }
librc_hidden_def(rc_service_value_get) librc_hidden_def(rc_service_value_get)

View File

@ -237,7 +237,7 @@ static struct mntent *getmntfile (const char *file)
static char **find_mounts (struct args *args) static char **find_mounts (struct args *args)
{ {
FILE *fp; FILE *fp;
char buffer[PATH_MAX * 3]; char *buffer;
char *p; char *p;
char *from; char *from;
char *to; char *to;
@ -250,7 +250,8 @@ static char **find_mounts (struct args *args)
if ((fp = fopen ("/proc/mounts", "r")) == NULL) if ((fp = fopen ("/proc/mounts", "r")) == NULL)
eerrorx ("getmntinfo: %s", strerror (errno)); eerrorx ("getmntinfo: %s", strerror (errno));
while (fgets (buffer, sizeof (buffer), fp)) { buffer = xmalloc (sizeof (char) * PATH_MAX * 3);
while (fgets (buffer, PATH_MAX * 3, fp)) {
netdev = -1; netdev = -1;
p = buffer; p = buffer;
from = strsep (&p, " "); from = strsep (&p, " ");
@ -265,6 +266,7 @@ static char **find_mounts (struct args *args)
process_mount (&list, args, from, to, fst, opts, netdev); process_mount (&list, args, from, to, fst, opts, netdev);
} }
free (buffer);
fclose (fp); fclose (fp);
return (list); return (list);

View File

@ -40,7 +40,7 @@ static char *applet = NULL;
#include "_usage.h" #include "_usage.h"
#define getoptstring "t:suT" getoptstring_COMMON #define getoptstring "t:suT" getoptstring_COMMON
static struct option longopts[] = { static struct option longopts[] = {
{ "type", 0, NULL, 't'}, { "type", 1, NULL, 't'},
{ "notrace", 0, NULL, 'T'}, { "notrace", 0, NULL, 'T'},
{ "strict", 0, NULL, 's'}, { "strict", 0, NULL, 's'},
{ "update", 0, NULL, 'u'}, { "update", 0, NULL, 'u'},
@ -67,7 +67,7 @@ int rc_depend (int argc, char **argv)
bool first = true; bool first = true;
int i; int i;
bool update = false; bool update = false;
char *runlevel = getenv ("RC_SOFTLEVEL"); char *runlevel = xstrdup( getenv ("RC_SOFTLEVEL"));
int opt; int opt;
char *token; char *token;
@ -104,12 +104,12 @@ int rc_depend (int argc, char **argv)
eerrorx ("Failed to update the dependency tree"); eerrorx ("Failed to update the dependency tree");
} }
if (! runlevel)
runlevel = rc_runlevel_get ();
if (! (deptree = _rc_deptree_load ())) if (! (deptree = _rc_deptree_load ()))
eerrorx ("failed to load deptree"); eerrorx ("failed to load deptree");
if (! runlevel)
runlevel = rc_runlevel_get ();
while (optind < argc) { while (optind < argc) {
list = NULL; list = NULL;
rc_strlist_add (&list, argv[optind]); rc_strlist_add (&list, argv[optind]);
@ -128,6 +128,7 @@ int rc_depend (int argc, char **argv)
if (! services) { if (! services) {
rc_strlist_free (types); rc_strlist_free (types);
rc_deptree_free (deptree); rc_deptree_free (deptree);
free (runlevel);
if (update) if (update)
return (EXIT_SUCCESS); return (EXIT_SUCCESS);
eerrorx ("no services specified"); eerrorx ("no services specified");
@ -159,5 +160,6 @@ int rc_depend (int argc, char **argv)
rc_strlist_free (services); rc_strlist_free (services);
rc_strlist_free (depends); rc_strlist_free (depends);
rc_deptree_free (deptree); rc_deptree_free (deptree);
free (runlevel);
return (EXIT_SUCCESS); return (EXIT_SUCCESS);
} }

View File

@ -132,7 +132,7 @@ char **env_filter (void)
static bool file_regex (const char *file, const char *regex) static bool file_regex (const char *file, const char *regex)
{ {
FILE *fp; FILE *fp;
char buffer[RC_LINEBUFFER]; char *buffer;
regex_t re; regex_t re;
bool retval = false; bool retval = false;
int result; int result;
@ -140,10 +140,12 @@ static bool file_regex (const char *file, const char *regex)
if (! (fp = fopen (file, "r"))) if (! (fp = fopen (file, "r")))
return (false); return (false);
buffer = xmalloc (sizeof (char) * RC_LINEBUFFER);
if ((result = regcomp (&re, regex, REG_EXTENDED | REG_NOSUB)) != 0) { if ((result = regcomp (&re, regex, REG_EXTENDED | REG_NOSUB)) != 0) {
fclose (fp); fclose (fp);
regerror (result, &re, buffer, sizeof (buffer)); regerror (result, &re, buffer, RC_LINEBUFFER);
fprintf (stderr, "file_regex: %s", buffer); fprintf (stderr, "file_regex: %s", buffer);
free (buffer);
return (false); return (false);
} }
@ -154,6 +156,7 @@ static bool file_regex (const char *file, const char *regex)
break; break;
} }
} }
free (buffer);
fclose (fp); fclose (fp);
regfree (&re); regfree (&re);

View File

@ -150,15 +150,16 @@ void rc_plugin_run (rc_hook_t hook, const char *value)
rc_in_plugin = true; rc_in_plugin = true;
exit (retval); exit (retval);
} else { } else {
char buffer[RC_LINEBUFFER]; char *buffer;
char *token; char *token;
char *p; char *p;
ssize_t nr; ssize_t nr;
close (pfd[1]); close (pfd[1]);
memset (buffer, 0, sizeof (buffer)); buffer = xmalloc (sizeof (char) * RC_LINEBUFFER);
memset (buffer, 0, RC_LINEBUFFER);
while ((nr = read (pfd[0], buffer, sizeof (buffer))) > 0) { while ((nr = read (pfd[0], buffer, RC_LINEBUFFER)) > 0) {
p = buffer; p = buffer;
while (*p && p - buffer < nr) { while (*p && p - buffer < nr) {
token = strsep (&p, "="); token = strsep (&p, "=");
@ -173,6 +174,7 @@ void rc_plugin_run (rc_hook_t hook, const char *value)
} }
} }
free (buffer);
close (pfd[0]); close (pfd[0]);
} }
} }

View File

@ -382,7 +382,7 @@ static int do_options (int argc, char **argv)
static char *proc_getent (const char *ent) static char *proc_getent (const char *ent)
{ {
FILE *fp; FILE *fp;
char buffer[RC_LINEBUFFER]; char *buffer;
char *p; char *p;
char *value = NULL; char *value = NULL;
int i; int i;
@ -395,7 +395,8 @@ static char *proc_getent (const char *ent)
return (NULL); return (NULL);
} }
memset (buffer, 0, sizeof (buffer)); buffer = xmalloc (sizeof (char) * RC_LINEBUFFER);
memset (buffer, 0, RC_LINEBUFFER);
if (fgets (buffer, RC_LINEBUFFER, fp) && if (fgets (buffer, RC_LINEBUFFER, fp) &&
(p = strstr (buffer, ent))) (p = strstr (buffer, ent)))
{ {
@ -413,6 +414,7 @@ static char *proc_getent (const char *ent)
} }
} else } else
errno = ENOENT; errno = ENOENT;
free (buffer);
fclose (fp); fclose (fp);
return (value); return (value);

View File

@ -376,7 +376,7 @@ static bool svc_exec (const char *arg1, const char *arg2)
int flags = 0; int flags = 0;
fd_set rset; fd_set rset;
int s; int s;
char buffer[RC_LINEBUFFER]; char *buffer;
size_t bytes; size_t bytes;
bool prefixed = false; bool prefixed = false;
int selfd; int selfd;
@ -437,6 +437,7 @@ static bool svc_exec (const char *arg1, const char *arg2)
} }
selfd = MAX (master_tty, signal_pipe[0]) + 1; selfd = MAX (master_tty, signal_pipe[0]) + 1;
buffer = xmalloc (sizeof (char) * RC_LINEBUFFER);
while (1) { while (1) {
FD_ZERO (&rset); FD_ZERO (&rset);
FD_SET (signal_pipe[0], &rset); FD_SET (signal_pipe[0], &rset);
@ -456,12 +457,13 @@ static bool svc_exec (const char *arg1, const char *arg2)
break; break;
if (master_tty >= 0 && FD_ISSET (master_tty, &rset)) { if (master_tty >= 0 && FD_ISSET (master_tty, &rset)) {
bytes = read (master_tty, buffer, sizeof (buffer)); bytes = read (master_tty, buffer, RC_LINEBUFFER);
write_prefix (buffer, bytes, &prefixed); write_prefix (buffer, bytes, &prefixed);
} }
} }
} }
free (buffer);
close (signal_pipe[0]); close (signal_pipe[0]);
close (signal_pipe[1]); close (signal_pipe[1]);
signal_pipe[0] = signal_pipe[1] = -1; signal_pipe[0] = signal_pipe[1] = -1;