优化gnss部分未判断的问题
authorCaisking <Casiking@zhihuishixun.com>
Sat, 24 Jan 2026 09:03:24 +0000 (17:03 +0800)
committerCaisking <Casiking@zhihuishixun.com>
Sat, 24 Jan 2026 09:03:24 +0000 (17:03 +0800)
code/sdk/zhihui/services/gnss/sample/pgnss_sample/pgnss_sample.c

index 7ffc61c..5dbcada 100644 (file)
@@ -100,6 +100,11 @@ static void get_current_timestamp(char *timestamp_str, size_t max_len) {
     
     time(&rawtime);
     timeinfo = localtime(&rawtime);
+    if (timeinfo == NULL) {
+        printf("Error: localtime() returned NULL\n");
+        snprintf(timestamp_str, max_len, "1970-01-01 00:00:00");
+        return;
+    }
     
     snprintf(timestamp_str, max_len, "%04d-%02d-%02d %02d:%02d:%02d",
              timeinfo->tm_year + 1900,
@@ -117,6 +122,11 @@ static void get_current_timestamp_ms(char *timestamp_str, size_t max_len) {
     
     gettimeofday(&tv, NULL);
     timeinfo = localtime(&tv.tv_sec);
+    if (timeinfo == NULL) {
+        printf("Error: localtime() returned NULL\n");
+        snprintf(timestamp_str, max_len, "1970-01-01 00:00:00.000");
+        return;
+    }
     
     snprintf(timestamp_str, max_len, "%04d-%02d-%02d %02d:%02d:%02d.%03ld",
              timeinfo->tm_year + 1900,
@@ -420,13 +430,17 @@ int32_t InjectPgnssWithFallbackToColdStart(tiot_handle handle)
 
     time(&rawtime);          // 获取UTC时间戳
     timeinfo = gmtime(&rawtime);  // 转换为UTC时间(无时区偏移)
-
-    utcTime.year = timeinfo->tm_year + 1900;
-    utcTime.month = timeinfo->tm_mon + 1;
-    utcTime.day = timeinfo->tm_mday;
-    utcTime.hour = timeinfo->tm_hour;
-    utcTime.minute = timeinfo->tm_min;
-    utcTime.second = timeinfo->tm_sec;
+    if (timeinfo == NULL) {
+        printf("Error: gmtime() returned NULL\n");
+        return 0;
+    } else {
+        utcTime.year = timeinfo->tm_year + 1900;
+        utcTime.month = timeinfo->tm_mon + 1;
+        utcTime.day = timeinfo->tm_mday;
+        utcTime.hour = timeinfo->tm_hour;
+        utcTime.minute = timeinfo->tm_min;
+        utcTime.second = timeinfo->tm_sec;
+    }
 
     printf("year[%d], mon[%d], day[%d], hour[%d], min[%d], sec[%d] \r\n",
     utcTime.year,utcTime.month,utcTime.day,
@@ -556,8 +570,13 @@ static void ListenGnssData(tiot_handle handle)
             
             uint16_t *type = (uint16_t *)g_gnssServiceBuff;
             if (type[0] == MSG_TYPE_ASCII) {
-
-                g_gnssServiceBuff[ret] = '\0';
+                // 确保ret在有效范围内再写入结束符
+                if (ret > 0 && ret < REPORT_MAX_BYTES) {
+                    g_gnssServiceBuff[ret] = '\0';
+                } else {
+                    printf("Error: Invalid ret value: %d, skipping this data\n", ret);
+                    continue;
+                }
                 
                 if (ret > MSG_CONTENT_IDX) {
                     char* pbuf = (char *)&g_gnssServiceBuff[MSG_CONTENT_IDX];
@@ -768,7 +787,7 @@ void PGnssServerStop(void)
         return;
     }
     
-    // 启动定时器(100ms间隔)
+    // 启动定时器(1000ms间隔)
     int tick = osMs2Tick(1000);
     osStatus_t ret = osTimerStart(g_stop_wait_timer, tick);
     if (ret != osOK) {