Browse Source

tm1637: example: avoid buffer overflow and possible NULL derefs

defects 398, 399

Signed-off-by: Jon Trulson <jtrulson@ics.com>
Signed-off-by: Mihai Tudor Panu <mihai.tudor.panu@intel.com>
Jon Trulson 9 years ago
parent
commit
a1dac4228a
1 changed files with 4 additions and 2 deletions
  1. 4
    2
      examples/c++/tm1637.cxx

+ 4
- 2
examples/c++/tm1637.cxx View File

@@ -47,7 +47,7 @@ main(int argc, char** argv)
47 47
 	int timezone = -7; // Your UTC offset
48 48
 	time_t rawtime;
49 49
 	struct tm * gmt;
50
-	char myTime[4];
50
+	char myTime[5];
51 51
 
52 52
     fprintf(stdout, "TM1637 Display Example\n");
53 53
     signal(SIGINT, sig_handler);
@@ -62,8 +62,10 @@ main(int argc, char** argv)
62 62
     	time(&rawtime); // Update raw time
63 63
         gmt = gmtime(&rawtime); // Get current time
64 64
 
65
+        int hour = (gmt) ? gmt->tm_hour : 0;
66
+        int min = (gmt) ? gmt->tm_min : 0;
65 67
         // Format and store the time in 24 hour format
66
-        sprintf(myTime, "%2d%02d", (gmt->tm_hour + timezone + 24) % 24, gmt->tm_min);
68
+        snprintf(myTime, 5, "%2d%02d", (hour + timezone + 24) % 24, min);
67 69
 
68 70
         myDisplay.write(myTime); // Write to display as string
69 71
     	myDisplay.setColon(point ^= true); // Toggle the dots on the display