ソースを参照

Refactor fgets checking code into fgets_must and fgets_checked functions

Ian Jackson 18 年 前
コミット
b95907e6e0
共有4 個のファイルを変更した47 個の追加22 個の削除を含む
  1. 8 0
      ChangeLog
  2. 3 0
      lib/dpkg.h
  3. 33 0
      lib/utils.c
  4. 3 22
      src/filesdb.c

+ 8 - 0
ChangeLog

@@ -1,3 +1,11 @@
+2008-03-11  Ian Jackson  <ian@davenant.greenend.org.uk>
+
+	* lib/dpkg.h (fgets_checked, fgets_must): New function declarations.
+	* lib/utils.c: Include <string.h>.
+	(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  <ian@davenant.greenend.org.uk>
 
 	* dselect/pkgdisplay.cc (relatestrings): Change 'breaks with' to

+ 3 - 0
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 {

+ 33 - 0
lib/utils.c

@@ -20,6 +20,7 @@
 
 #include <config.h>
 #include <dpkg.h>
+#include <string.h>
 
 /* 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;
+}
+

+ 3 - 22
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;