use getline instead of fgets

The getline function allocate the needed memory. Fix buffer can lead
to 'hard to find' bug. I don't test the pivot_root part but the other
parts are ok.

Signed-off-by: Clement Calmels <clement.calmels@fr.ibm.com>
Signed-off-by: Daniel Lezcano <dlezcano@fr.ibm.com>
This commit is contained in:
Clement Calmels 2010-01-18 23:08:12 +01:00 committed by Daniel Lezcano
parent 8eec72f75d
commit 2382ecffdb
4 changed files with 15 additions and 18 deletions

View File

@ -146,7 +146,6 @@ static int configure_find_fstype_cb(char* buffer, void *data)
static int configure_find_fstype(const char *rootfs, char *fstype, int mntopt) static int configure_find_fstype(const char *rootfs, char *fstype, int mntopt)
{ {
int i, found; int i, found;
char buffer[MAXPATHLEN];
struct cbarg { struct cbarg {
const char *rootfs; const char *rootfs;
@ -182,7 +181,7 @@ static int configure_find_fstype(const char *rootfs, char *fstype, int mntopt)
found = lxc_file_for_each_line(fsfile[i], found = lxc_file_for_each_line(fsfile[i],
configure_find_fstype_cb, configure_find_fstype_cb,
buffer, sizeof(buffer), &cbarg); &cbarg);
if (found < 0) { if (found < 0) {
SYSERROR("failed to read '%s'", fsfile[i]); SYSERROR("failed to read '%s'", fsfile[i]);
@ -396,7 +395,7 @@ static int setup_rootfs_pivot_root_cb(char *buffer, void *data)
static int setup_rootfs_pivot_root(const char *rootfs, const char *pivotdir) static int setup_rootfs_pivot_root(const char *rootfs, const char *pivotdir)
{ {
char path[MAXPATHLEN], buffer[MAXPATHLEN]; char path[MAXPATHLEN];
void *cbparm[2]; void *cbparm[2];
struct lxc_list mountlist, *iterator; struct lxc_list mountlist, *iterator;
int ok, still_mounted, last_still_mounted; int ok, still_mounted, last_still_mounted;
@ -458,9 +457,7 @@ static int setup_rootfs_pivot_root(const char *rootfs, const char *pivotdir)
} }
snprintf(path, sizeof(path), "/%s/proc/mounts", pivotdir); snprintf(path, sizeof(path), "/%s/proc/mounts", pivotdir);
ok = lxc_file_for_each_line(path, ok = lxc_file_for_each_line(path, setup_rootfs_pivot_root_cb, &cbparm);
setup_rootfs_pivot_root_cb,
buffer, sizeof(buffer), &cbparm);
if (ok < 0) { if (ok < 0) {
SYSERROR("failed to read or parse mount list '%s'", path); SYSERROR("failed to read or parse mount list '%s'", path);
return -1; return -1;

View File

@ -666,10 +666,7 @@ int lxc_config_readline(char *buffer, struct lxc_conf *conf)
int lxc_config_read(const char *file, struct lxc_conf *conf) int lxc_config_read(const char *file, struct lxc_conf *conf)
{ {
char buffer[MAXPATHLEN]; return lxc_file_for_each_line(file, parse_line, conf);
return lxc_file_for_each_line(file, parse_line, buffer,
sizeof(buffer), conf);
} }
int lxc_config_define_add(struct lxc_list *defines, char* arg) int lxc_config_define_add(struct lxc_list *defines, char* arg)

View File

@ -64,11 +64,12 @@ int lxc_dir_for_each(const char *name, const char *directory,
return ret; return ret;
} }
int lxc_file_for_each_line(const char *file, lxc_file_cb callback, int lxc_file_for_each_line(const char *file, lxc_file_cb callback, void *data)
char *buffer, size_t len, void* data)
{ {
FILE *f; FILE *f;
int err = 0; int err = 0;
char *line = NULL;
size_t len = 0;
f = fopen(file, "r"); f = fopen(file, "r");
if (!f) { if (!f) {
@ -76,14 +77,16 @@ int lxc_file_for_each_line(const char *file, lxc_file_cb callback,
return -1; return -1;
} }
while (fgets(buffer, len, f)) { while (getline(&line, &len, f) != -1) {
err = callback(buffer, data); err = callback(line, data);
if (err) { if (err) {
ERROR("failed to process '%s'", buffer); ERROR("failed to process '%s'", line);
goto out; break;
} }
} }
out:
if (line)
free(line);
fclose(f); fclose(f);
return err; return err;
} }

View File

@ -32,7 +32,7 @@ extern int lxc_dir_for_each(const char *name, const char *directory,
lxc_dir_cb callback, void *data); lxc_dir_cb callback, void *data);
extern int lxc_file_for_each_line(const char *file, lxc_file_cb callback, extern int lxc_file_for_each_line(const char *file, lxc_file_cb callback,
char *buffer, size_t len, void* data); void* data);
extern int lxc_char_left_gc(char *buffer, size_t len); extern int lxc_char_left_gc(char *buffer, size_t len);