@@ -56,10 +56,10 @@ SDL_Surface* weatherImage;
5656SDL_Texture * weatherTexture ;
5757
5858
59- char * weathertemp = "00°F" ;
60- char * weathericon = "romfs:/gfx/unknown.png" ;
61- char * weathertext = "unknown" ;
62- char * weatherlocation = "unknown location" ;
59+ char weathertemp [ 256 ] ;
60+ char weathericon [ 256 ] ;
61+ char weathertext [ 256 ] ;
62+ char weatherlocation [ 256 ] ;
6363
6464static inline SDL_Color SDL_MakeColour (Uint8 r , Uint8 g , Uint8 b , Uint8 a )
6565{
@@ -112,7 +112,7 @@ void SDL_DrawTextf(SDL_Renderer *renderer, TTF_Font *font, int x, int y, SDL_Col
112112 char buffer [256 ];
113113 va_list args ;
114114 va_start (args , text );
115- vsnprintf (buffer , 256 , text , args );
115+ vsnprintf (buffer , sizeof ( buffer ) , text , args );
116116 SDL_DrawText (renderer , font , x , y , colour , buffer );
117117 va_end (args );
118118}
@@ -157,7 +157,7 @@ char *Clock(void) {
157157
158158
159159 //Clock
160- char * clock = "time" ;
160+ char clock [ 256 ] ;
161161 const char * ampm = "AM" ;
162162 time_t unixTime = time (NULL );
163163 struct tm * timeStruct = localtime ((const time_t * )& unixTime );
@@ -181,7 +181,7 @@ char *Clock(void) {
181181 ampm = "PM" ;
182182 }
183183
184- snprintf (clock , 256 , "%s %s %i %i %02i:%02i:%02i %s" , weekDays [wday ], months [month ], day , year , hours , minutes , seconds , ampm );
184+ snprintf (clock , sizeof ( clock ) + 1 , "%s %s %i %i %02i:%02i:%02i %s" , weekDays [wday ], months [month ], day , year , hours , minutes , seconds , ampm );
185185 SDL_DrawTextf (renderer , font , SCREEN_WIDTH / 2 , 3 , CYAN , clock );
186186
187187 return (clock );
@@ -306,10 +306,10 @@ void *getjson(char *JsonString) {
306306 jsmn_parser p ;
307307 jsmntok_t t [256 ]; /* We expect no more than 128 tokens */
308308
309- char * gettemp = "" ;
310- char * getweather = "" ;
311- char * getweatherimage = "" ;
312- char * getlocation = "" ;
309+ char gettemp [ 256 ] ;
310+ char getweather [ 256 ] ;
311+ char getweatherimage [ 256 ] ;
312+ char getlocation [ 256 ] ;
313313
314314
315315 jsmn_init (& p );
@@ -330,24 +330,24 @@ void *getjson(char *JsonString) {
330330 for (i = 1 ; i < r ; i ++ ) {
331331 if (jsoneq (JsonString , & t [i ], "Temp" ) == 0 ) {
332332 //printf("Temp: %.*s\n", t[i+1].end-t[i+1].start, JsonString + t[i+1].start);
333- snprintf (gettemp , 256 , "%.*s" , t [i + 1 ].end - t [i + 1 ].start , JsonString + t [i + 1 ].start );
333+ snprintf (gettemp , sizeof ( gettemp ) + 1 , "%.*s" , t [i + 1 ].end - t [i + 1 ].start , JsonString + t [i + 1 ].start );
334334 i ++ ;
335335 }
336336
337337 else if (jsoneq (JsonString , & t [i ], "Weather" ) == 0 ) {
338338 //printf("Weather: %.*s\n", t[i+1].end-t[i+1].start, JsonString + t[i+1].start);
339- snprintf (getweather , 256 , "%.*s" , t [i + 1 ].end - t [i + 1 ].start , JsonString + t [i + 1 ].start );
339+ snprintf (getweather , sizeof ( getweather ) + 1 , "%.*s" , t [i + 1 ].end - t [i + 1 ].start , JsonString + t [i + 1 ].start );
340340 i ++ ;
341341 } else if (jsoneq (JsonString , & t [i ], "Weatherimage" ) == 0 ) {
342342 //printf("Weatherimage: %.*s\n", t[i+1].end-t[i+1].start, JsonString + t[i+1].start);
343- snprintf (getweatherimage , 256 , "%.*s" , t [i + 1 ].end - t [i + 1 ].start , JsonString + t [i + 1 ].start );
343+ snprintf (getweatherimage , sizeof ( getweatherimage ) + 1 , "%.*s" , t [i + 1 ].end - t [i + 1 ].start , JsonString + t [i + 1 ].start );
344344 i ++ ;
345345 }
346346
347347
348348 else if (jsoneq (JsonString , & t [i ], "name" ) == 0 ) {
349349 //printf("Location: %.*s\n", t[i+1].end-t[i+1].start, JsonString + t[i+1].start);
350- snprintf (getlocation , 256 , "%.*s" , t [i + 1 ].end - t [i + 1 ].start , JsonString + t [i + 1 ].start );
350+ snprintf (getlocation , sizeof ( getlocation ) + 1 , "%.*s" , t [i + 1 ].end - t [i + 1 ].start , JsonString + t [i + 1 ].start );
351351 i ++ ;
352352 }
353353
@@ -364,20 +364,20 @@ void *getjson(char *JsonString) {
364364 SDL_DrawTextf (renderer , font , SCREEN_WIDTH / 2 , 3 , CYAN , myclock );
365365
366366
367- snprintf (weathertemp , 256 , "%s°F" , gettemp );
367+ snprintf (weathertemp , sizeof ( weathertemp ) + 1 , "%s°F" , gettemp );
368368 SDL_DrawTextf (renderer , lgfont , SCREEN_WIDTH / 2 , SCREEN_HEIGHT - 30 , CYAN , weathertemp );
369369
370370
371- snprintf (weathericon , 256 , "romfs:/gfx/%s" , getweatherimage );
371+ snprintf (weathericon , sizeof ( weathericon ) + 1 , "romfs:/gfx/%s" , getweatherimage );
372372 weatherImage = IMG_Load (weathericon );
373373 weatherTexture = SDL_CreateTextureFromSurface (renderer , weatherImage );
374374 SDL_FreeSurface (weatherImage );
375375
376- snprintf (weathertext , 256 , "%s" , getweather );
376+ snprintf (weathertext , sizeof ( weathertext ) + 1 , "%s" , getweather );
377377 SDL_DrawTextf (renderer , font , SCREEN_WIDTH / 2 , SCREEN_HEIGHT + 73 , CYAN , weathertext );
378378
379379
380- snprintf (weatherlocation , 256 , "%s" , getlocation );
380+ snprintf (weatherlocation , sizeof ( weatherlocation ) + 1 , "%s" , getlocation );
381381 SDL_DrawTextf (renderer , font , SCREEN_WIDTH / 2 , SCREEN_HEIGHT + 136 , CYAN , weatherlocation );
382382
383383
@@ -523,7 +523,7 @@ int main()
523523 lat = strtok (mygps , "," );
524524 lon = strtok (NULL , "," );
525525 char gpsstring [256 ];
526- snprintf (gpsstring , 256 , "My GPS: %s,%s" , lat ,lon );
526+ snprintf (gpsstring , sizeof ( gpsstring ) + 1 , "My GPS: %s,%s" , lat ,lon );
527527 SDL_DrawTextf (renderer , font , SCREEN_WIDTH / 2 , 43 , CYAN , gpsstring );
528528
529529
0 commit comments