From c89909b0d4e3fc914045bc5bacf029699c1d2420 Mon Sep 17 00:00:00 2001 From: Matthias Andree Date: Tue, 31 May 2022 20:39:40 +0200 Subject: netrc: fix error routing and add I/O error checks --- netrc.c | 38 +++++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-) (limited to 'netrc.c') diff --git a/netrc.c b/netrc.c index 661da361..dadcae2f 100644 --- a/netrc.c +++ b/netrc.c @@ -17,6 +17,7 @@ #include #include #include +#include #include "netrc.h" #include "i18n.h" @@ -84,6 +85,7 @@ parse_netrc (char *file) const char *premature_token; netrc_entry *current, *retval; int ln; + int error_flag = 0; /* The latest token we've seen in the file. */ enum @@ -97,6 +99,9 @@ parse_netrc (char *file) if (!fp) { /* Just return NULL if we can't open the file. */ + if (ENOENT != errno) { + report(stderr, "%s: cannot open file for reading: %s\n", file, strerror(errno)); + } return NULL; } @@ -216,7 +221,7 @@ parse_netrc (char *file) if (premature_token) { - fprintf (stderr, + report(stderr, GT_("%s:%d: warning: found \"%s\" before any host names\n"), file, ln, premature_token); premature_token = NULL; @@ -255,19 +260,33 @@ parse_netrc (char *file) else { - fprintf (stderr, GT_("%s:%d: warning: unknown token \"%s\"\n"), + report(stderr, GT_("%s:%d: warning: unknown token \"%s\"\n"), file, ln, tok); } } } } - fclose (fp); + if (ferror(fp)) { + report(stderr, GT_("%s: error reading file (%s).\n"), file, strerror(errno)); + error_flag = 1; + clearerr(fp); + } + + if (fclose(fp)) { + report(stderr, GT_("%s: error reading file (%s).\n"), file, strerror(errno)); + error_flag = 1; + } /* Finalize the last machine entry we found. */ maybe_add_to_list (¤t, &retval); free (current); + if (error_flag) { + free_netrc(retval); + return NULL; + } + /* Reverse the order of the list so that it appears in file order. */ current = retval; retval = NULL; @@ -345,21 +364,22 @@ int main (int argc, char **argv) case 4: break; default: - fprintf (stderr, "Usage: %s [ ]\n", argv[0]); + fprintf(stderr, "Usage: %s [ ]\n", argv[0]); exit(EXIT_FAILURE); } - if (stat (file, &sb)) + report_init(1); + + if (stat(file, &sb)) { - fprintf (stderr, "%s: cannot stat %s: %s\n", argv[0], file, + fprintf(stderr, "PRE-CHECK for %s: cannot stat %s: %s\n", argv[0], file, strerror (errno)); - exit (1); } - head = parse_netrc (file); + head = parse_netrc(file); if (!head) { - fprintf (stderr, "%s: no entries found in %s\n", argv[0], file); + fprintf(stderr, "%s: read error or no entries found in %s\n", argv[0], file); exit (1); } -- cgit v1.2.3