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

View File

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

View File

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

View File

@ -157,21 +157,23 @@ librc_hidden_def(rc_runlevel_list)
char *rc_runlevel_get (void)
{
FILE *fp;
char buffer[RC_LINEBUFFER];
char *runlevel = NULL;
if ((fp = fopen (SOFTLEVEL, "r"))) {
if (fgets (buffer, PATH_MAX, fp)) {
int i = strlen (buffer) - 1;
if (buffer[i] == '\n')
buffer[i] = 0;
runlevel = xstrdup (buffer);
}
runlevel = xmalloc (sizeof (char) * PATH_MAX);
if (fgets (runlevel, PATH_MAX, fp)) {
int i = strlen (runlevel) - 1;
if (runlevel[i] == '\n')
runlevel[i] = 0;
} else
*runlevel = '\0';
fclose (fp);
}
if (! runlevel)
if (! runlevel || ! *runlevel) {
free (runlevel);
runlevel = xstrdup (RC_LEVEL_SYSINIT);
}
return (runlevel);
}
@ -272,8 +274,8 @@ librc_hidden_def(rc_service_exists)
char **rc_service_options (const char *service)
{
char *svc;
char cmd[PATH_MAX];
char buffer[RC_LINEBUFFER];
char *cmd = NULL;
char *buffer = NULL;
char **opts = NULL;
char *token;
char *p = buffer;
@ -282,18 +284,19 @@ char **rc_service_options (const char *service)
if (! (svc = rc_service_resolve (service)))
return (NULL);
snprintf (cmd, sizeof (cmd), ". '%s'; echo \"${opts}\"", svc);
asprintf (&cmd, ". '%s'; echo \"${opts}\"", svc);
free (svc);
if (! (fp = popen (cmd, "r")))
return (NULL);
if (fgets (buffer, RC_LINEBUFFER, fp)) {
if (buffer[strlen (buffer) - 1] == '\n')
buffer[strlen (buffer) - 1] = '\0';
while ((token = strsep (&p, " ")))
rc_strlist_addsort (&opts, token);
if ((fp = popen (cmd, "r"))) {
buffer = xmalloc (sizeof (char) * RC_LINEBUFFER);
if (fgets (buffer, RC_LINEBUFFER, fp)) {
if (buffer[strlen (buffer) - 1] == '\n')
buffer[strlen (buffer) - 1] = '\0';
while ((token = strsep (&p, " ")))
rc_strlist_addsort (&opts, token);
}
pclose (fp);
free (buffer);
}
pclose (fp);
return (opts);
}
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 *svc;
char cmd[PATH_MAX];
char buffer[RC_LINEBUFFER];
char *cmd = NULL;
char *buffer;
char *desc = NULL;
FILE *fp;
int i;
@ -313,25 +316,25 @@ char *rc_service_description (const char *service, const char *option)
if (! option)
option = "";
snprintf (cmd, sizeof (cmd), ". '%s'; echo \"${description%s%s}\"",
asprintf (&cmd, ". '%s'; echo \"${description%s%s}\"",
svc, option ? "_" : "", option);
free (svc);
if (! (fp = popen (cmd, "r")))
return (NULL);
while (fgets (buffer, RC_LINEBUFFER, fp)) {
if (! desc) {
desc = xmalloc (strlen (buffer) + 1);
*desc = '\0';
} else {
desc = xrealloc (desc, strlen (desc) + strlen (buffer) + 1);
if ((fp = popen (cmd, "r"))) {
buffer = xmalloc (sizeof (char) * RC_LINEBUFFER);
while (fgets (buffer, RC_LINEBUFFER, fp)) {
if (! desc) {
desc = xmalloc (strlen (buffer) + 1);
*desc = '\0';
} else {
desc = xrealloc (desc, strlen (desc) + strlen (buffer) + 1);
}
i = strlen (desc);
memcpy (desc + i, buffer, strlen (buffer));
memset (desc + i + strlen (buffer), 0, 1);
}
i = strlen (desc);
memcpy (desc + i, buffer, strlen (buffer));
memset (desc + i + strlen (buffer), 0, 1);
free (buffer);
pclose (fp);
}
pclose (fp);
return (desc);
}
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)
{
FILE *fp;
char buffer[RC_LINEBUFFER];
char *buffer = NULL;
char *file = rc_strcatpaths (RC_SVCDIR, "options", service, option,
(char *) NULL);
char *value = NULL;
if ((fp = fopen (file, "r"))) {
memset (buffer, 0, sizeof (buffer));
if (fgets (buffer, RC_LINEBUFFER, fp))
value = xstrdup (buffer);
buffer = xmalloc (sizeof (char) * RC_LINEBUFFER);
fgets (buffer, RC_LINEBUFFER, fp);
fclose (fp);
}
free (file);
return (value);
return (buffer);
}
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)
{
FILE *fp;
char buffer[PATH_MAX * 3];
char *buffer;
char *p;
char *from;
char *to;
@ -250,7 +250,8 @@ static char **find_mounts (struct args *args)
if ((fp = fopen ("/proc/mounts", "r")) == NULL)
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;
p = buffer;
from = strsep (&p, " ");
@ -265,6 +266,7 @@ static char **find_mounts (struct args *args)
process_mount (&list, args, from, to, fst, opts, netdev);
}
free (buffer);
fclose (fp);
return (list);

View File

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

View File

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

View File

@ -150,15 +150,16 @@ void rc_plugin_run (rc_hook_t hook, const char *value)
rc_in_plugin = true;
exit (retval);
} else {
char buffer[RC_LINEBUFFER];
char *buffer;
char *token;
char *p;
ssize_t nr;
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;
while (*p && p - buffer < nr) {
token = strsep (&p, "=");
@ -172,7 +173,8 @@ void rc_plugin_run (rc_hook_t hook, const char *value)
}
}
}
free (buffer);
close (pfd[0]);
}
}

View File

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

View File

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