[Genabel-commits] r1211 - pkg/ProbABEL/src

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri May 10 23:38:05 CEST 2013


Author: lckarssen
Date: 2013-05-10 23:38:05 +0200 (Fri, 10 May 2013)
New Revision: 1211

Modified:
   pkg/ProbABEL/src/gendata.cpp
Log:
In ProbABEL's gendata.cpp: Cleaner implementation of the conversion from string to double when reading the genetic data (both from text files and .fv? files. Based on the strtod and strtol man pages. 
Now Vlagrind doesn't complain about jumps based on uninitialised values anymore. Furthermore, the invalied read sizes of 8 bytes have also gone.


Modified: pkg/ProbABEL/src/gendata.cpp
===================================================================
--- pkg/ProbABEL/src/gendata.cpp	2013-05-10 11:54:05 UTC (rev 1210)
+++ pkg/ProbABEL/src/gendata.cpp	2013-05-10 21:38:05 UTC (rev 1211)
@@ -5,6 +5,7 @@
  *      Author: mkooyman
  */
 #include <string>
+#include <errno.h>
 #include "gendata.h"
 #include "fvlib/FileVector.h"
 #if EIGEN
@@ -41,7 +42,26 @@
                 std::ostringstream strs;
                 strs << tmpdata[i];
                 std::string str = strs.str();
-                data[j++] = strtod(str.c_str(), (char **) NULL);
+                double val;
+                char *endptr;
+                errno = 0;      // To distinguish success/failure
+                                // after strtod()
+                val = strtod(str.c_str(), &endptr);
+
+                if ((errno == ERANGE && (val == HUGE_VALF || val == HUGE_VALL))
+                    || (errno != 0 && val == 0)) {
+                    perror("Error while reading genetic data (strtod)");
+                    exit(EXIT_FAILURE);
+                }
+
+                if (endptr == str.c_str()) {
+                    cerr << "No digits were found while reading genetic data"
+                         << endl;
+                    exit(EXIT_FAILURE);
+                }
+
+                /* If we got here, strtod() successfully parsed a number */
+                data[j++] = val;
             }
         }
     }
@@ -171,9 +191,26 @@
                     infile >> tmpstr;
                     // tmpstr contains the dosage in string form. Convert
                     // it to double (if tmpstr is NaN it will be set to nan).
-                    // Note that Valgrind 3.7.0 gives "Invalid read of
-                    // size 8" error messages here. A bug in Valgrind?!
-                    double dosage = strtod(tmpstr.c_str(), (char **) NULL);
+                    double dosage;
+                    char *endptr;
+                    errno = 0;      // To distinguish success/failure
+                                    // after strtod()
+                    dosage = strtod(tmpstr.c_str(), &endptr);
+
+                    if ((errno == ERANGE &&
+                         (dosage == HUGE_VALF || dosage == HUGE_VALL))
+                        || (errno != 0 && dosage == 0)) {
+                        perror("Error while reading genetic data (strtod)");
+                        exit(EXIT_FAILURE);
+                    }
+
+                    if (endptr == tmpstr.c_str()) {
+                        cerr << "No digits were found while reading genetic data"
+                             << endl;
+                        exit(EXIT_FAILURE);
+                    }
+
+                    /* If we got here, strtod() successfully parsed a number */
                     G.put(dosage, k, j);
                 }
                 else



More information about the Genabel-commits mailing list