fixes for minor issues identified by coverity
Costa Tsaousis (ktsaou) committed
Oct 15, 2017 at 21:18 UTC
e1b47faa4f5f93f85c50fa1fd29bfac71bed895d
1 file changed
+18
-11
src/main.c
+18
-11
@@ -503,7 +503,7 @@ static void get_system_timezone(void) {
503
if(!tz || !*tz)
504
setenv("TZ", config_get(CONFIG_SECTION_GLOBAL, "TZ environment variable", ":/etc/localtime"), 0);
505
506
- char buffer[FILENAME_MAX + 1];
506
+ char buffer[FILENAME_MAX + 1] = "";
507
const char *timezone = NULL;
508
ssize_t ret;
509
@@ -520,17 +520,23 @@ static void get_system_timezone(void) {
520
}
521
522
// read the link /etc/localtime
523
- if(!timezone && (ret = readlink("/etc/localtime", buffer, FILENAME_MAX)) > 0) {
524
- buffer[ret] = '\0';
523
+ if(!timezone) {
524
+ ret = readlink("/etc/localtime", buffer, FILENAME_MAX);
525
+
526
+ if(ret > 0) {
527
+ buffer[ret] = '\0';
528
526
- char *cmp = "/usr/share/zoneinfo/";
527
- size_t cmp_len = strlen(cmp);
529
+ char *cmp = "/usr/share/zoneinfo/";
530
+ size_t cmp_len = strlen(cmp);
531
529
- char *s = strstr(buffer, cmp);
530
- if(s && s[cmp_len]) {
531
- timezone = &s[cmp_len];
532
- // info("TIMEZONE: using the link of /etc/localtime: '%s'", timezone);
532
+ char *s = strstr(buffer, cmp);
533
+ if (s && s[cmp_len]) {
534
+ timezone = &s[cmp_len];
535
+ // info("TIMEZONE: using the link of /etc/localtime: '%s'", timezone);
536
+ }
537
}
538
+ else
539
+ buffer[0] = '\0';
540
}
541
542
// find the timezone from strftime()
@@ -556,7 +562,8 @@ static void get_system_timezone(void) {
562
// make sure it does not have illegal characters
563
// info("TIMEZONE: fixing '%s'", timezone);
564
559
- char tmp[strlen(timezone) + 1];
565
+ size_t len = strlen(timezone);
566
+ char tmp[len + 1];
567
char *d = tmp;
568
*d = '\0';
569
@@ -567,7 +574,7 @@ static void get_system_timezone(void) {
574
timezone++;
575
}
576
*d = '\0';
570
- strcpy(buffer, tmp);
577
+ strncpyz(buffer, tmp, len);
578
timezone = buffer;
579
// info("TIMEZONE: fixed as '%s'", timezone);
580
}