mirror of
https://git.zx2c4.com/cgit
synced 2024-11-23 08:58:41 +00:00
Use strbuf for reading configuration files
Use struct strbuf from Git instead of fixed-size buffers to remove the limit on the length of configuration file lines and refactor read_config_line() to improve readability. Note that this also fixes a buffer overflow that existed with the original fixed-size buffer implementation. Signed-off-by: Lukas Fleischer <cgit@cryptocrack.de>
This commit is contained in:
parent
820df9c660
commit
50e70d32f0
62
configfile.c
62
configfile.c
@ -31,45 +31,45 @@ static void skip_line(FILE *f)
|
|||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int read_config_line(FILE *f, char *line, const char **value, int bufsize)
|
static int read_config_line(FILE *f, struct strbuf *name, struct strbuf *value)
|
||||||
{
|
{
|
||||||
int i = 0, isname = 0;
|
|
||||||
|
|
||||||
*value = NULL;
|
|
||||||
while (i < bufsize - 1) {
|
|
||||||
int c = next_char(f);
|
int c = next_char(f);
|
||||||
if (!isname && (c == '#' || c == ';')) {
|
|
||||||
skip_line(f);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (!isname && isspace(c))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
if (c == '=' && !*value) {
|
strbuf_reset(name);
|
||||||
line[i] = 0;
|
strbuf_reset(value);
|
||||||
*value = &line[i + 1];
|
|
||||||
} else if (c == '\n' && !isname) {
|
/* Skip comments and preceding spaces. */
|
||||||
i = 0;
|
for(;;) {
|
||||||
continue;
|
if (c == '#' || c == ';')
|
||||||
} else if (c == '\n' || c == EOF) {
|
skip_line(f);
|
||||||
line[i] = 0;
|
else if (!isspace(c))
|
||||||
break;
|
break;
|
||||||
} else {
|
c = next_char(f);
|
||||||
line[i] = c;
|
|
||||||
}
|
}
|
||||||
isname = 1;
|
|
||||||
i++;
|
/* Read variable name. */
|
||||||
|
while (c != '=') {
|
||||||
|
if (c == '\n' || c == EOF)
|
||||||
|
return 0;
|
||||||
|
strbuf_addch(name, c);
|
||||||
|
c = next_char(f);
|
||||||
}
|
}
|
||||||
line[i + 1] = 0;
|
|
||||||
return i;
|
/* Read variable value. */
|
||||||
|
c = next_char(f);
|
||||||
|
while (c != '\n' && c != EOF) {
|
||||||
|
strbuf_addch(value, c);
|
||||||
|
c = next_char(f);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int parse_configfile(const char *filename, configfile_value_fn fn)
|
int parse_configfile(const char *filename, configfile_value_fn fn)
|
||||||
{
|
{
|
||||||
static int nesting;
|
static int nesting;
|
||||||
int len;
|
struct strbuf name = STRBUF_INIT;
|
||||||
char line[256];
|
struct strbuf value = STRBUF_INIT;
|
||||||
const char *value;
|
|
||||||
FILE *f;
|
FILE *f;
|
||||||
|
|
||||||
/* cancel deeply nested include-commands */
|
/* cancel deeply nested include-commands */
|
||||||
@ -78,10 +78,12 @@ int parse_configfile(const char *filename, configfile_value_fn fn)
|
|||||||
if (!(f = fopen(filename, "r")))
|
if (!(f = fopen(filename, "r")))
|
||||||
return -1;
|
return -1;
|
||||||
nesting++;
|
nesting++;
|
||||||
while ((len = read_config_line(f, line, &value, sizeof(line))) > 0)
|
while (read_config_line(f, &name, &value))
|
||||||
fn(line, value);
|
fn(name.buf, value.buf);
|
||||||
nesting--;
|
nesting--;
|
||||||
fclose(f);
|
fclose(f);
|
||||||
|
strbuf_release(&name);
|
||||||
|
strbuf_release(&value);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
#ifndef CONFIGFILE_H
|
#ifndef CONFIGFILE_H
|
||||||
#define CONFIGFILE_H
|
#define CONFIGFILE_H
|
||||||
|
|
||||||
|
#include "cgit.h"
|
||||||
|
|
||||||
typedef void (*configfile_value_fn)(const char *name, const char *value);
|
typedef void (*configfile_value_fn)(const char *name, const char *value);
|
||||||
|
|
||||||
extern int parse_configfile(const char *filename, configfile_value_fn fn);
|
extern int parse_configfile(const char *filename, configfile_value_fn fn);
|
||||||
|
Loading…
Reference in New Issue
Block a user