瀏覽代碼

Check for return value of scanf

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@73983 15284696-431f-4ddb-bdfa-cd5b030d7da7
Maris Nartiss 6 年之前
父節點
當前提交
28cdd30147
共有 2 個文件被更改,包括 42 次插入19 次删除
  1. 36 18
      lib/imagery/sigset.c
  2. 6 1
      lib/raster3d/rle.c

+ 36 - 18
lib/imagery/sigset.c

@@ -129,11 +129,14 @@ int I_ReadSigSet(FILE * fd, struct SigSet *S)
 
     while (gettag(fd, tag)) {
 	if (eq(tag, "title:"))
-	    get_title(fd, S);
+	    if (get_title(fd, S) != 0)
+            return -1;
 	if (eq(tag, "nbands:"))
-	    get_nbands(fd, S);
+	    if (get_nbands(fd, S) != 0)
+            return -1;
 	if (eq(tag, "class:"))
-	    get_class(fd, S);
+	    if (get_class(fd, S) != 0)
+            return -1;
     }
     return 1;			/* for now assume success */
 }
@@ -148,7 +151,8 @@ static int gettag(FILE * fd, char *tag)
 
 static int get_nbands(FILE * fd, struct SigSet *S)
 {
-    fscanf(fd, "%d", &S->nbands);
+    if (fscanf(fd, "%d", &S->nbands) != 1)
+        return -1;
 
     return 0;
 }
@@ -158,7 +162,8 @@ static int get_title(FILE * fd, struct SigSet *S)
     char title[1024];
 
     *title = 0;
-    fscanf(fd, "%[^\n]", title);
+    if (fscanf(fd, "%[^\n]", title) != 1)
+        return -1;
     I_SetSigTitle(S, title);
 
     return 0;
@@ -174,13 +179,17 @@ static int get_class(FILE * fd, struct SigSet *S)
 	if (eq(tag, "endclass:"))
 	    break;
 	if (eq(tag, "classnum:"))
-	    get_classnum(fd, C);
+	    if (get_classnum(fd, C) != 0)
+            return -1;
 	if (eq(tag, "classtype:"))
-	    get_classtype(fd, C);
+	    if (get_classtype(fd, C) != 0)
+            return -1;
 	if (eq(tag, "classtitle:"))
-	    get_classtitle(fd, C);
+	    if (get_classtitle(fd, C) != 0)
+            return -1;
 	if (eq(tag, "subclass:"))
-	    get_subclass(fd, S, C);
+	    if (get_subclass(fd, S, C) != 0)
+            return -1;
     }
 
     return 0;
@@ -188,14 +197,16 @@ static int get_class(FILE * fd, struct SigSet *S)
 
 static int get_classnum(FILE * fd, struct ClassSig *C)
 {
-    fscanf(fd, "%ld", &C->classnum);
+    if (fscanf(fd, "%ld", &C->classnum) != 1)
+        return -1;
 
     return 0;
 }
 
 static int get_classtype(FILE * fd, struct ClassSig *C)
 {
-    fscanf(fd, "%d", &C->type);
+    if (fscanf(fd, "%d", &C->type) != 1)
+        return -1;
 
     return 0;
 }
@@ -205,7 +216,8 @@ static int get_classtitle(FILE * fd, struct ClassSig *C)
     char title[1024];
 
     *title = 0;
-    fscanf(fd, "%[^\n]", title);
+    if (fscanf(fd, "%[^\n]", title) != 1)
+        return -1;
     I_SetClassTitle(C, title);
 
     return 0;
@@ -222,11 +234,14 @@ static int get_subclass(FILE * fd, struct SigSet *S, struct ClassSig *C)
 	if (eq(tag, "endsubclass:"))
 	    break;
 	if (eq(tag, "pi:"))
-	    get_subclass_pi(fd, Sp);
+	    if (get_subclass_pi(fd, Sp) != 0)
+            return -1;
 	if (eq(tag, "means:"))
-	    get_subclass_means(fd, Sp, S->nbands);
+	    if (get_subclass_means(fd, Sp, S->nbands) != 0)
+            return -1;
 	if (eq(tag, "covar:"))
-	    get_subclass_covar(fd, Sp, S->nbands);
+	    if (get_subclass_covar(fd, Sp, S->nbands) != 0)
+            return -1;
     }
 
     return 0;
@@ -234,7 +249,8 @@ static int get_subclass(FILE * fd, struct SigSet *S, struct ClassSig *C)
 
 static int get_subclass_pi(FILE * fd, struct SubSig *Sp)
 {
-    fscanf(fd, "%lf", &Sp->pi);
+    if (fscanf(fd, "%lf", &Sp->pi) != 1)
+        return -1;
 
     return 0;
 }
@@ -244,7 +260,8 @@ static int get_subclass_means(FILE * fd, struct SubSig *Sp, int nbands)
     int i;
 
     for (i = 0; i < nbands; i++)
-	fscanf(fd, "%lf", &Sp->means[i]);
+	if (fscanf(fd, "%lf", &Sp->means[i]) != 1)
+        return -1;
 
     return 0;
 }
@@ -255,7 +272,8 @@ static int get_subclass_covar(FILE * fd, struct SubSig *Sp, int nbands)
 
     for (i = 0; i < nbands; i++)
 	for (j = 0; j < nbands; j++)
-	    fscanf(fd, "%lf", &Sp->R[i][j]);
+	    if (fscanf(fd, "%lf", &Sp->R[i][j]) != 1)
+            return -1;
 
     return 0;
 }

+ 6 - 1
lib/raster3d/rle.c

@@ -264,6 +264,10 @@ Rast3d_rle_decode(char *src, char *dst, int nofElts, int eltLength,
 
 /*---------------------------------------------------------------------------*/
 
+/* TODO: Find out if this function used at all.
+ * Seems to be some leftover from the early pre-SVN days of GRASS GIS.
+ * Maris, 2018.
+ */
 void test_rle()
 {
     char c[100];
@@ -271,7 +275,8 @@ void test_rle()
 
     do {
 	printf("length? ");
-	scanf("%d", &length);
+	if (scanf("%d", &length) != 1)
+        Rast3d_fatal_error("Error reading length");
 	printf("length = %d\n", length);
 	printf("codeLength %d   ", G_rle_codeLength(length));
 	(void)rle_length2code(length, c);