Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Avoid overflow values

Verifiy that strtod did not return +/- HUGE_VAL
  • Loading branch information...
commit 87e0dbe5d082da905940f0505ab88cae25497772 1 parent 54f2bd5
Daniel Kalotay authored December 07, 2011

Showing 1 changed file with 11 additions and 7 deletions. Show diff stats Hide diff stats

  1. 18  spark.c
18  spark.c
@@ -11,7 +11,7 @@ int main(int argc, char* argv[])
11 11
     int n_elements = 0;
12 12
     double elements[MAX_INPUT];
13 13
     double min_element = HUGE_VAL;
14  
-    double max_element = - HUGE_VAL;
  14
+    double max_element = -HUGE_VAL;
15 15
 
16 16
     if (argc < 2)
17 17
         return -1;
@@ -29,13 +29,17 @@ int main(int argc, char* argv[])
29 29
                 ++to_be_processed;
30 30
             /* strtod did recognise a number */
31 31
             } else {
32  
-                elements[n_elements++] = value;
33  
-                min_element = value < min_element ? value : min_element;
34  
-                max_element = value > max_element ? value : max_element;
  32
+                /* avoid overflow */
  33
+                /* TODO: deal with underflow */
  34
+                if (value != HUGE_VAL && value != -HUGE_VAL) {
  35
+                    elements[n_elements++] = value;
  36
+                    min_element = value < min_element ? value : min_element;
  37
+                    max_element = value > max_element ? value : max_element;
35 38
 
36  
-                /* array full; get out of here */
37  
-                if (n_elements == MAX_INPUT)
38  
-                    goto cleanup;
  39
+                    /* array full; get out of here */
  40
+                    if (n_elements == MAX_INPUT)
  41
+                        goto cleanup;
  42
+                }
39 43
 
40 44
                 /* to_be_processed was shifted to point to the character
41 45
                  * following the last on of the number. Shift process_from to

0 notes on commit 87e0dbe

Please sign in to comment.
Something went wrong with that request. Please try again.