From 63146072a4e7e85fa962eaf7d0d51a109cd66058 Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Sun, 15 May 2016 16:32:38 +0200 Subject: [PATCH] Fix getline(3) and getdelim(3) usage. --- disked/disked.c | 20 +++++++++----------- editor/editor.c | 4 ++-- init/init.c | 6 +++--- kblayout-compiler/kblayout-compiler.c | 6 ++++-- libc/fstab/getfsent.c | 4 ++-- mkinitrd/rules.c | 12 ++++++------ sh/proper-sh.c | 5 ++++- sysinstall/conf.c | 4 ++-- sysinstall/devices.c | 4 ++-- sysinstall/manifest.c | 14 +++++++------- sysinstall/release.c | 4 ++-- tix/porttix-create.c | 5 ++++- tix/srctix-create.c | 5 ++++- tix/tix-collection.c | 1 + tix/tix-execdiff.c | 1 + tix/tix-install.c | 4 +++- tix/util.h | 18 +++++++++++------- utils/colormake.c | 3 +++ utils/column.c | 14 +++++++------- utils/sort.c | 2 +- utils/tail.c | 5 +++-- utils/uniq.c | 2 +- 22 files changed, 82 insertions(+), 61 deletions(-) diff --git a/disked/disked.c b/disked/disked.c index b0569fb3..e8c943b1 100644 --- a/disked/disked.c +++ b/disked/disked.c @@ -715,9 +715,9 @@ static bool lookup_fstab_by_blockdevice(struct fstab* out_fsent, char* line = NULL; size_t line_size = 0; ssize_t line_length; - while ( 0 <= (errno = 0, line_length = getline(&line, &line_size, fstab_fp)) ) + while ( 0 < (line_length = getline(&line, &line_size, fstab_fp)) ) { - if ( line_length && line[line_length - 1] == '\n' ) + if ( line[line_length - 1] == '\n' ) line[--line_length] = '\0'; if ( !scanfsent(line, out_fsent) ) continue; @@ -730,8 +730,6 @@ static bool lookup_fstab_by_blockdevice(struct fstab* out_fsent, } free(line); fclose(fstab_fp); - if ( errno ) - return false; return false; } @@ -747,9 +745,9 @@ static bool remove_blockdevice_from_fstab(struct blockdevice* bdev) char* line = NULL; size_t line_size = 0; ssize_t line_length; - while ( 0 <= (errno = 0, line_length = getline(&line, &line_size, rewr.in)) ) + while ( 0 < (line_length = getline(&line, &line_size, rewr.in)) ) { - if ( line_length && line[line_length - 1] == '\n' ) + if ( line[line_length - 1] == '\n' ) line[--line_length] = '\0'; char* dup = strdup(line); if ( !dup ) @@ -761,7 +759,7 @@ static bool remove_blockdevice_from_fstab(struct blockdevice* bdev) free(dup); } free(line); - if ( errno ) + if ( ferror(rewr.in) ) return rewrite_abort(&rewr), false; return rewrite_finish(&rewr); } @@ -811,9 +809,9 @@ static bool add_blockdevice_to_fstab(struct blockdevice* bdev, size_t line_size = 0; ssize_t line_length; bool found = false; - while ( 0 <= (errno = 0, line_length = getline(&line, &line_size, rewr.in)) ) + while ( 0 < (line_length = getline(&line, &line_size, rewr.in)) ) { - if ( line_length && line[line_length - 1] == '\n' ) + if ( line[line_length - 1] == '\n' ) line[--line_length] = '\0'; char* dup = strdup(line); if ( !dup ) @@ -845,7 +843,7 @@ static bool add_blockdevice_to_fstab(struct blockdevice* bdev, free(dup); } free(line); - if ( errno ) + if ( ferror(rewr.in) ) return rewrite_abort(&rewr), false; if ( !found ) print_blockdevice_fsent(rewr.out, bdev, mountpoint); @@ -2820,7 +2818,7 @@ int main(int argc, char* argv[]) break; } - if ( line_length && line[line_length-1] == '\n' ) + if ( line[line_length-1] == '\n' ) line[--line_length] = '\0'; execute(line); diff --git a/editor/editor.c b/editor/editor.c index 147a7e20..e50a25de 100644 --- a/editor/editor.c +++ b/editor/editor.c @@ -97,7 +97,7 @@ void editor_load_config_path(struct editor* editor, const char* path) char* line = NULL; size_t line_size = 0; ssize_t line_length = 0; - while ( 0 <= (errno = 0, line_length = getline(&line, &line_size, fp)) ) + while ( 0 < (line_length = getline(&line, &line_size, fp)) ) { if ( line[line_length - 1] == '\n' ) line[--line_length] = '\0'; @@ -105,7 +105,7 @@ void editor_load_config_path(struct editor* editor, const char* path) line[line_length] = '\0'; editor_modal_command_config(editor, line); } - if ( errno != 0 ) + if ( ferror(fp) ) error(0, errno, "getline: %s", path); fclose(fp); } diff --git a/init/init.c b/init/init.c index b27f8460..1ed41376 100644 --- a/init/init.c +++ b/init/init.c @@ -63,7 +63,7 @@ static char* read_single_line(FILE* fp) free(ret); return NULL; } - if ( ret_length && ret[ret_length-1] == '\n' ) + if ( ret[ret_length-1] == '\n' ) ret[--ret_length] = '\0'; return ret; } @@ -326,7 +326,7 @@ static void load_fstab(void) char* line = NULL; size_t line_size; ssize_t line_length; - while ( 0 < (errno = 0, line_length = getline(&line, &line_size, fp)) ) + while ( 0 < (line_length = getline(&line, &line_size, fp)) ) { if ( line[line_length - 1] == '\n' ) line[--line_length] = '\0'; @@ -354,7 +354,7 @@ static void load_fstab(void) line = NULL; line_size = 0; } - if ( errno ) + if ( ferror(fp) ) fatal("/etc/fstab: %m"); free(line); fclose(fp); diff --git a/kblayout-compiler/kblayout-compiler.c b/kblayout-compiler/kblayout-compiler.c index 8659536d..377b8088 100644 --- a/kblayout-compiler/kblayout-compiler.c +++ b/kblayout-compiler/kblayout-compiler.c @@ -460,9 +460,9 @@ int main(int argc, char* argv[]) char* line = NULL; size_t line_size = 0; ssize_t line_length; - while ( 0 <= (line_length = getline(&line, &line_size, input)) ) + while ( 0 < (line_length = getline(&line, &line_size, input)) ) { - if ( line_length && line[line_length-1] == '\n' ) + if ( line[line_length-1] == '\n' ) line[--line_length] = '\0'; if ( line[0] == '/' && line[1] == '/' ) continue; @@ -604,6 +604,8 @@ int main(int argc, char* argv[]) assert(found_key); } free(line); + if ( ferror(input) ) + error(1, errno, "%s", input_path); fclose(input); if ( verbose ) diff --git a/libc/fstab/getfsent.c b/libc/fstab/getfsent.c index 265f81b9..a67254a6 100644 --- a/libc/fstab/getfsent.c +++ b/libc/fstab/getfsent.c @@ -29,9 +29,9 @@ struct fstab* getfsent(void) static char* line = NULL; static size_t line_size = 0; ssize_t line_length; - while ( 0 <= (line_length = getline(&line, &line_size, __fstab_file)) ) + while ( 0 < (line_length = getline(&line, &line_size, __fstab_file)) ) { - if ( line_length && line[line_length - 1] == '\n' ) + if ( line[line_length - 1] == '\n' ) line[--line_length] = '\0'; if ( scanfsent(line, &fs) ) return &fs; diff --git a/mkinitrd/rules.c b/mkinitrd/rules.c index ff217634..ff4f03da 100644 --- a/mkinitrd/rules.c +++ b/mkinitrd/rules.c @@ -174,12 +174,12 @@ bool AddRulesFromFile(FILE* fp, const char* fpname) size_t line_num = 0; char* mem = NULL; ssize_t line_len; - while ( 0 <= (line_len = getline(&mem, &line_size, fp)) ) + while ( 0 < (line_len = getline(&mem, &line_size, fp)) ) { char* line = mem; line_num++; - if ( line_len && line[line_len-1] == '\n' ) - line[line_len-1] = '\0'; + if ( line[line_len-1] == '\n' ) + line[--line_len] = '\0'; line = (char*) SkipWhitespace((char*) line); if ( IsLineComment(line) ) continue; @@ -279,10 +279,10 @@ bool AddManifestFromFile(FILE* fp, const char* fpname) char* line = NULL; size_t line_size = 0; ssize_t line_len; - while ( 0 <= (line_len = getline(&line, &line_size, fp)) ) + while ( 0 < (line_len = getline(&line, &line_size, fp)) ) { - if ( line_len && line[line_len-1] == '\n' ) - line[line_len-1] = '\0'; + if ( line[line_len-1] == '\n' ) + line[--line_len] = '\0'; if ( !AddManifestPath(line) ) return false; } diff --git a/sh/proper-sh.c b/sh/proper-sh.c index 70091168..3f3d60d8 100644 --- a/sh/proper-sh.c +++ b/sh/proper-sh.c @@ -91,8 +91,11 @@ char* search_for_proper_shell(void) ssize_t result_length = getline(&result, &result_size, fp); fclose(fp); if ( result_length < 0 ) + { + free(result); continue; - if ( result_length && result[result_length-1] == '\n' ) + } + if ( result[result_length-1] == '\n' ) result[--result_length] = '\0'; if ( !is_existing_shell(result) ) { diff --git a/sysinstall/conf.c b/sysinstall/conf.c index 8be31eea..bc8d5483 100644 --- a/sysinstall/conf.c +++ b/sysinstall/conf.c @@ -74,7 +74,7 @@ void load_upgrade_conf(struct conf* conf, const char* path) char* line = NULL; size_t line_size = 0; ssize_t line_length; - while ( 0 < (errno = 0, line_length = getline(&line, &line_size, fp)) ) + while ( 0 < (line_length = getline(&line, &line_size, fp)) ) { if ( line[line_length - 1] == '\n' ) line[--line_length] = '\0'; @@ -105,7 +105,7 @@ void load_upgrade_conf(struct conf* conf, const char* path) name[name_length] = '\0'; conf_assign(conf, name, value, path); } - if ( errno ) + if ( ferror(fp) ) err(2, "%s", path); free(line); fclose(fp); diff --git a/sysinstall/devices.c b/sysinstall/devices.c index 7ad033ea..6cc48f67 100644 --- a/sysinstall/devices.c +++ b/sysinstall/devices.c @@ -272,7 +272,7 @@ bool load_mountpoints(const char* fstab_path, char* line = NULL; size_t line_size; ssize_t line_length; - while ( 0 < (errno = 0, line_length = getline(&line, &line_size, fp)) ) + while ( 0 < (line_length = getline(&line, &line_size, fp)) ) { if ( line[line_length - 1] == '\n' ) line[--line_length] = '\0'; @@ -309,7 +309,7 @@ bool load_mountpoints(const char* fstab_path, line = NULL; line_size = 0; } - bool failure = errno; + bool failure = ferror(fp); free(line); fclose(fp); if ( failure ) diff --git a/sysinstall/manifest.c b/sysinstall/manifest.c index fd079c5f..6ba96b21 100644 --- a/sysinstall/manifest.c +++ b/sysinstall/manifest.c @@ -97,9 +97,9 @@ void install_manifest(const char* manifest, char* line = NULL; size_t line_size = 0; ssize_t line_length; - while ( 0 <= (errno = 0, line_length = getline(&line, &line_size, fpin)) ) + while ( 0 < (line_length = getline(&line, &line_size, fpin)) ) { - if ( line_length && line[line_length-1] == '\n' ) + if ( line[line_length-1] == '\n' ) line[--line_length] = '\0'; if ( fprintf(fpout, "%s\n", line) < 0 ) { @@ -241,7 +241,7 @@ void install_manifest(const char* manifest, free(out_path); } free(line); - if ( errno ) + if ( ferror(fpin) ) { warn("%s", inmanifest); _exit(2); @@ -273,7 +273,7 @@ bool check_installed(const char* path, const char* package) char* line = NULL; size_t line_size = 0; ssize_t line_length; - while ( 0 < (errno = 0, line_length = getline(&line, &line_size, fp)) ) + while ( 0 < (line_length = getline(&line, &line_size, fp)) ) { if ( line[line_length-1] == '\n' ) line[--line_length] = '\0'; @@ -284,7 +284,7 @@ bool check_installed(const char* path, const char* package) return true; } } - if ( errno != 0 ) + if ( ferror(fp) ) warn("%s", path); free(line); fclose(fp); @@ -363,7 +363,7 @@ void install_ports(const char* from_prefix, const char* to_prefix) char* line = NULL; size_t line_size = 0; ssize_t line_length; - while ( 0 < (errno = 0, line_length = getline(&line, &line_size, fp)) ) + while ( 0 < (line_length = getline(&line, &line_size, fp)) ) { if ( line[line_length-1] == '\n' ) line[--line_length] = '\0'; @@ -395,7 +395,7 @@ void install_ports(const char* from_prefix, const char* to_prefix) install_manifest(line, from_prefix, to_prefix); } free(line); - if ( errno ) + if ( ferror(fp) ) { warn("%s", cmd); pclose(fp); diff --git a/sysinstall/release.c b/sysinstall/release.c index 66d8c140..3582db39 100644 --- a/sysinstall/release.c +++ b/sysinstall/release.c @@ -110,7 +110,7 @@ bool os_release_load(struct release* release, char* line = NULL; size_t line_size = 0; ssize_t line_length; - while ( 0 < (errno = 0, line_length = getline(&line, &line_size, fp)) ) + while ( 0 < (line_length = getline(&line, &line_size, fp)) ) { if ( line[line_length-1] == '\n' ) line[--line_length] = '\0'; @@ -178,7 +178,7 @@ bool os_release_load(struct release* release, } } } - if ( errno ) + if ( ferror(fp) ) warn("%s", errpath); else if ( failure ) ; diff --git a/tix/porttix-create.c b/tix/porttix-create.c index 2657d897..2768bcd8 100644 --- a/tix/porttix-create.c +++ b/tix/porttix-create.c @@ -21,6 +21,7 @@ #include #include +#include #include #include #include @@ -313,7 +314,7 @@ int main(int argc, char* argv[]) ssize_t line_len; while ( 0 < (line_len = getline(&line, &line_size, tar_fp)) ) { - if ( line_len && line[line_len-1] == '\n' ) + if ( line[line_len-1] == '\n' ) line[--line_len] = '\0'; const char* path = line; while ( *path && *path != '/' ) @@ -335,6 +336,8 @@ int main(int argc, char* argv[]) } } free(line); + if ( ferror(tar_fp) ) + error(1, errno, "getline: tar"); fclose(tar_fp); int tar_exit_status; diff --git a/tix/srctix-create.c b/tix/srctix-create.c index b1d1546a..6e7dad69 100644 --- a/tix/srctix-create.c +++ b/tix/srctix-create.c @@ -21,6 +21,7 @@ #include #include +#include #include #include #include @@ -154,7 +155,7 @@ int main(int argc, char* argv[]) ssize_t line_len; while ( 0 < (line_len = getline(&line, &line_size, porttixinfo_fp)) ) { - if ( line_len && line[line_len-1] == '\n' ) + if ( line[line_len-1] == '\n' ) line[--line_len] = '\0'; char* first_space = strchr(line, ' '); if ( !first_space ) @@ -271,6 +272,8 @@ int main(int argc, char* argv[]) porttixinfo_path, function); } free(line); + if ( ferror(porttixinfo_fp) ) + error(1, errno, "%s", porttixinfo_path); fclose(porttixinfo_fp); free(porttixinfo_path); diff --git a/tix/tix-collection.c b/tix/tix-collection.c index 1698525e..cf31707e 100644 --- a/tix/tix-collection.c +++ b/tix/tix-collection.c @@ -21,6 +21,7 @@ #include #include +#include #include #include #include diff --git a/tix/tix-execdiff.c b/tix/tix-execdiff.c index decb6818..a368a997 100644 --- a/tix/tix-execdiff.c +++ b/tix/tix-execdiff.c @@ -21,6 +21,7 @@ #include #include +#include #include #include #include diff --git a/tix/tix-install.c b/tix/tix-install.c index 5843e54f..5ce9b482 100644 --- a/tix/tix-install.c +++ b/tix/tix-install.c @@ -105,7 +105,7 @@ bool IsPackageInstalled(const char* tixdb_path, const char* package) ssize_t line_len; while ( 0 < (line_len = getline(&line, &line_size, installed_list_fp)) ) { - if ( line_len && line[line_len-1] == '\n' ) + if ( line[line_len-1] == '\n' ) line[--line_len] = '\0'; if ( !strcmp(line, package) ) { @@ -114,6 +114,8 @@ bool IsPackageInstalled(const char* tixdb_path, const char* package) } } free(line); + if ( ferror(installed_list_fp) ) + error(1, errno, "`%s'", installed_list_path); fclose(installed_list_fp); free(installed_list_path); diff --git a/tix/util.h b/tix/util.h index 2fc7bc43..9af93220 100644 --- a/tix/util.h +++ b/tix/util.h @@ -242,11 +242,12 @@ void string_array_append_file(string_array_t* sa, FILE* fp) ssize_t entry_length; while ( 0 < (entry_length = getline(&entry, &entry_size, fp)) ) { - if ( entry_length && entry[entry_length-1] == '\n' ) - entry[entry_length-1] = '\0'; + if ( entry[entry_length-1] == '\n' ) + entry[--entry_length] = '\0'; string_array_append(sa, entry); } free(entry); + assert(!ferror(fp)); } bool string_array_append_file_path(string_array_t* sa, const char* path) @@ -330,14 +331,15 @@ void dictionary_append_file(string_array_t* sa, FILE* fp) ssize_t entry_length; while ( 0 < (entry_length = getline(&entry, &entry_size, fp)) ) { - if ( entry_length && entry[entry_length-1] == '\n' ) - entry[entry_length-1] = '\0'; + if ( entry[entry_length-1] == '\n' ) + entry[--entry_length] = '\0'; dictionary_normalize_entry(entry); if ( entry[0] == '#' ) continue; string_array_append(sa, entry); } free(entry); + assert(!ferror(fp)); } bool dictionary_append_file_path(string_array_t* sa, const char* path) @@ -372,8 +374,8 @@ char* read_single_line(FILE* fp) free(ret); return NULL; } - if ( ret_len && ret[ret_len-1] == '\n' ) - ret[ret_len-1] = '\0'; + if ( ret[ret_len-1] == '\n' ) + ret[--ret_len] = '\0'; return ret; } @@ -563,7 +565,7 @@ bool TarContainsFile(const char* archive, const char* file) bool ret = false; while ( 0 < (line_len = getline(&line, &line_size, fp)) ) { - if ( line_len && line[line_len-1] == '\n' ) + if ( line[line_len-1] == '\n' ) line[--line_len] = '\0'; if ( strcmp(line, file) == 0 ) { @@ -575,6 +577,8 @@ bool TarContainsFile(const char* archive, const char* file) } } free(line); + if ( ferror(fp) ) + error(1, errno, "getline: tar"); fclose(fp); int tar_exit_status; diff --git a/utils/colormake.c b/utils/colormake.c index 9d1dd4c4..aba545fa 100644 --- a/utils/colormake.c +++ b/utils/colormake.c @@ -97,9 +97,12 @@ int main(int argc, char* argv[]) mode = next_mode; } free(line); + int stdin_errno = errno; int status; waitpid(child_pid, &status, 0); printf("\e[m"); fflush(stdout); + if ( ferror(stdin) ) + error(1, stdin_errno, "stdin"); return WEXITSTATUS(status); } diff --git a/utils/column.c b/utils/column.c index 4cf906e9..ca4fd68a 100644 --- a/utils/column.c +++ b/utils/column.c @@ -95,14 +95,14 @@ bool append_lines_from_file(FILE* fp, if ( string_length < 0 ) { free(string); - if ( feof(stdin) ) - return true; - if ( errno == 0 ) - return true; - error(0, errno, "getline: `%s'", fpname); - return false; + if ( ferror(fp) ) + { + error(0, errno, "getline: `%s'", fpname); + return false; + } + return true; } - if ( string_length && string[string_length-1] == '\n' ) + if ( string[string_length-1] == '\n' ) string[--string_length] = '\0'; size_t display_width = measure_line_display_width(string); if ( display_width == 0 && !flag_empty ) diff --git a/utils/sort.c b/utils/sort.c index 82a43c2d..a5d4d41a 100644 --- a/utils/sort.c +++ b/utils/sort.c @@ -105,7 +105,7 @@ char* read_line(FILE* fp, const char* fpname, int delim) error(0, errno, "read: `%s'", fpname); return NULL; } - if ( amount && (unsigned char) line[amount-1] == (unsigned char) delim ) + if ( (unsigned char) line[amount-1] == (unsigned char) delim ) line[amount-1] = '\0'; return line; } diff --git a/utils/tail.c b/utils/tail.c index 00cd60f4..fbcecd7b 100644 --- a/utils/tail.c +++ b/utils/tail.c @@ -67,9 +67,10 @@ bool processfp(const char* inputname, FILE* fp) ssize_t linelen = getline(&line, &linesize, fp); if ( linelen < 0 ) { + if ( ferror(fp) ) + error(1, errno, "%s", inputname); free(line); - if ( feof(fp) ) { break; } - error(1, errno, "%s", inputname); + break; } if ( specialleading ) { diff --git a/utils/uniq.c b/utils/uniq.c index f34d75bc..600c20c3 100644 --- a/utils/uniq.c +++ b/utils/uniq.c @@ -41,7 +41,7 @@ char* read_line(FILE* fp, const char* fpname, int delim) error(0, errno, "read: `%s'", fpname); return NULL; } - if ( amount && (unsigned char) line[amount-1] == (unsigned char) delim ) + if ( (unsigned char) line[amount-1] == (unsigned char) delim ) line[amount-1] = '\0'; return line; }