aboutsummaryrefslogtreecommitdiff
path: root/src/librc/librc-misc.c
diff options
context:
space:
mode:
authorRoy Marples <roy@marples.name>2008-01-07 12:29:30 +0000
committerRoy Marples <roy@marples.name>2008-01-07 12:29:30 +0000
commit43d0f3fc76542d0859c9b84402c0483a22e02b68 (patch)
tree125bcc9bf644a6390718868312df2b6be1905e89 /src/librc/librc-misc.c
parent74e0e58b899accd2bd72a7d7303331e47089959f (diff)
rc_getline keeps expanding it's malloced buffer until it has read a whole line or EOF. All functions which read into static buffers have been changed to use fhis function to avoid any potential overflows and to ensure we really do read a long long config line.
Diffstat (limited to 'src/librc/librc-misc.c')
-rw-r--r--src/librc/librc-misc.c42
1 files changed, 26 insertions, 16 deletions
diff --git a/src/librc/librc-misc.c b/src/librc/librc-misc.c
index b8f59936..8cbd6a6d 100644
--- a/src/librc/librc-misc.c
+++ b/src/librc/librc-misc.c
@@ -105,6 +105,30 @@ char *rc_strcatpaths (const char *path1, const char *paths, ...)
}
librc_hidden_def(rc_strcatpaths)
+char *rc_getline (FILE *fp)
+{
+ char *line = NULL;
+ size_t len = 0;
+ size_t last = 0;
+
+ if (feof (fp))
+ return (NULL);
+
+ do {
+ len += BUFSIZ;
+ line = xrealloc (line, sizeof (char) * len);
+ fgets (line + last, BUFSIZ, fp);
+ last = strlen (line + last) - 1;
+ } while (! feof (fp) && line[last] != '\n');
+
+ /* Trim the trailing newline */
+ if (line[last] == '\n')
+ line[last] = '\0';
+
+ return (line);
+}
+librc_hidden_def(rc_getline)
+
char **rc_config_list (const char *file)
{
FILE *fp;
@@ -112,26 +136,12 @@ char **rc_config_list (const char *file)
char *p;
char *token;
char **list = NULL;
- size_t buflen = BUFSIZ;
- size_t last;
if (! (fp = fopen (file, "r")))
return (NULL);
- buffer = xmalloc (sizeof (char) * buflen);
- buffer[buflen - 1] = '\0';
- while (fgets (buffer, buflen, fp)) {
- /* Increase the buffer to read the rest of the line if needed */
- last = strlen (buffer) - 1;
- while (! feof (fp) && buffer[last] != '\n') {
- buflen += BUFSIZ;
- buffer = xrealloc (buffer, sizeof (char *) * buflen);
- fgets (buffer + last, BUFSIZ, fp);
- last = strlen (buffer) - 1;
- }
-
+ while ((p = buffer = rc_getline (fp))) {
/* Strip leading spaces/tabs */
- p = buffer;
while ((*p == ' ') || (*p == '\t'))
p++;
@@ -144,8 +154,8 @@ char **rc_config_list (const char *file)
rc_strlist_add (&list, token);
}
+ free (buffer);
}
- free (buffer);
fclose (fp);
return (list);