From b95907e6e0f3f25136fb2ebcc8d3489efb208dea Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Tue, 11 Mar 2008 05:10:53 +0200 Subject: [PATCH] Refactor fgets checking code into fgets_must and fgets_checked functions --- ChangeLog | 8 ++++++++ lib/dpkg.h | 3 +++ lib/utils.c | 33 +++++++++++++++++++++++++++++++++ src/filesdb.c | 25 +++---------------------- 4 files changed, 47 insertions(+), 22 deletions(-) diff --git a/ChangeLog b/ChangeLog index 8a2263ff..c5656bf8 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2008-03-11 Ian Jackson + + * lib/dpkg.h (fgets_checked, fgets_must): New function declarations. + * lib/utils.c: Include . + (fgets_checked, fgets_must): New function. + * src/filesdb.c (ensure_diversions): Use new fgets_checked and + fgets_must instead of duped code. + 2008-03-09 Ian Jackson * dselect/pkgdisplay.cc (relatestrings): Change 'breaks with' to diff --git a/lib/dpkg.h b/lib/dpkg.h index d0e15fe9..edb40098 100644 --- a/lib/dpkg.h +++ b/lib/dpkg.h @@ -371,6 +371,9 @@ void showcopyright(const struct cmdinfo*, const char*); int cisdigit(int c); int cisalpha(int c); +int fgets_checked(char *buf, size_t bufsz, FILE *f, const char *fn); +int fgets_must(char *buf, size_t bufsz, FILE *f, const char *fn); + /*** from compression.c ***/ enum compress_type { diff --git a/lib/utils.c b/lib/utils.c index f2a6a559..632df1ed 100644 --- a/lib/utils.c +++ b/lib/utils.c @@ -20,6 +20,7 @@ #include #include +#include /* Reimplementation of the standard ctype.h is* functions. Since gettext * has overloaded the meaning of LC_CTYPE we can't use that to force C @@ -32,3 +33,35 @@ int cisdigit(int c) { int cisalpha(int c) { return ((c>='a') && (c<='z')) || ((c>='A') && (c<='Z')); } + +int +fgets_checked(char *buf, size_t bufsz, FILE *f, const char *fn) +{ + int l; + + if (!fgets(buf, bufsz, f)) { + if (ferror(f)) + ohshite(_("read error in `%.250s'"), fn); + return -1; + } + l = strlen(buf); + if (l == 0) + ohshit(_("fgets gave an empty string from `%.250s'"), fn); + if (buf[--l] != '\n') + ohshit(_("too-long line or missing newline in `%.250s'"), fn); + buf[l] = 0; + + return l; +} + +int +fgets_must(char *buf, size_t bufsz, FILE *f, const char *fn) +{ + int l = fgets_checked(buf, bufsz, f, fn); + + if (l < 0) + ohshit(_("unexpected eof reading `%.250s'"), fn); + + return l; +} + diff --git a/src/filesdb.c b/src/filesdb.c index 425e7669..bc5973ba 100644 --- a/src/filesdb.c +++ b/src/filesdb.c @@ -470,37 +470,18 @@ void ensure_diversions(void) { diversions = NULL; if (!file) { onerr_abort--; return; } - while (fgets(linebuf,sizeof(linebuf),file)) { + while ((l = fgets_checked(linebuf, sizeof(linebuf), file, vb.buf)) >= 0) { oicontest= nfmalloc(sizeof(struct diversion)); oialtname= nfmalloc(sizeof(struct diversion)); - l= strlen(linebuf); - if (l == 0) ohshit(_("fgets gave an empty string from diversions [i]")); - if (linebuf[--l] != '\n') ohshit(_("diversions file has too-long line or EOF [i]")); - linebuf[l]= 0; oialtname->camefrom= findnamenode(linebuf, 0); oialtname->useinstead = NULL; - if (!fgets(linebuf,sizeof(linebuf),file)) { - if (ferror(file)) ohshite(_("read error in diversions [ii]")); - else ohshit(_("unexpected EOF in diversions [ii]")); - } - l= strlen(linebuf); - if (l == 0) ohshit(_("fgets gave an empty string from diversions [ii]")); - if (linebuf[--l] != '\n') ohshit(_("diversions file has too-long line or EOF [ii]")); - linebuf[l]= 0; + fgets_must(linebuf, sizeof(linebuf), file, vb.buf); oicontest->useinstead= findnamenode(linebuf, 0); oicontest->camefrom = NULL; - - if (!fgets(linebuf,sizeof(linebuf),file)) { - if (ferror(file)) ohshite(_("read error in diversions [iii]")); - else ohshit(_("unexpected EOF in diversions [iii]")); - } - l= strlen(linebuf); - if (l == 0) ohshit(_("fgets gave an empty string from diversions [iii]")); - if (linebuf[--l] != '\n') ohshit(_("diversions file has too-long line or EOF [ii]")); - linebuf[l]= 0; + fgets_must(linebuf, sizeof(linebuf), file, vb.buf); oicontest->pkg= oialtname->pkg= strcmp(linebuf, ":") ? findpackage(linebuf) : NULL; -- 2.39.5