Skip to content

Commit

Permalink
syslinux/extlinux: check if config file is empty
Browse files Browse the repository at this point in the history
For some reason (may be a user manual intervention or some issue on previous
updates) the syslinux/extlinux config file may be empty and in that case cbm will
segfault when reading the content's buffer.

Signed-off-by: Leandro Dorileo <[email protected]>
  • Loading branch information
Leandro Dorileo authored and bryteise committed Feb 12, 2020
1 parent 0f3c531 commit 2d9d985
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/bootloaders/extlinux.c
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ static bool extlinux_set_default_kernel(const BootManager *manager, const Kernel
}

/* If the file is the same, don't write it again or sync */
if (file_get_text(config_path, &old_conf)) {
if (cbm_file_has_content(config_path) && file_get_text(config_path, &old_conf)) {
if (streq(old_conf, writer->buffer)) {
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/bootloaders/syslinux.c
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ static bool syslinux_set_default_kernel(const BootManager *manager, const Kernel
}

/* If the file is the same, don't write it again or sync */
if (file_get_text(config_path, &old_conf)) {
if (cbm_file_has_content(config_path) && file_get_text(config_path, &old_conf)) {
if (streq(old_conf, writer->buffer)) {
return true;
}
Expand Down
26 changes: 26 additions & 0 deletions src/lib/files.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,32 @@ static bool get_parent_disk_devno(char *path, dev_t *diskdevno)
return true;
}

bool cbm_file_has_content(char *path)
{
int fd = -1;
struct stat st = { 0 };
ssize_t length = -1;

if (!nc_file_exists(path)) {
return false;
}

fd = open(path, O_RDONLY);
if (fd < 0) {
return false;
}

if (fstat(fd, &st) != 0) {
close(fd);
return false;
}

length = st.st_size;
close(fd);

return length != 0;
}

char *get_parent_disk(char *path)
{
dev_t devt;
Expand Down
5 changes: 5 additions & 0 deletions src/lib/files.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,11 @@ bool cbm_path_check(const char *path, const char *resolved);
*/
bool cbm_is_dir_empty(const char *path);

/**
* Check if @path exists and if it's empty.
*/
bool cbm_file_has_content(char *path);

/**
* Ensure a stack pointer vs a heap pointer, to save on copies
*/
Expand Down

0 comments on commit 2d9d985

Please sign in to comment.