修复ble_sywee_history内存泄漏问题 3449/1 3449/head 3451/target/1
authorluozheng <1732036298@qq.com>
Wed, 10 Jun 2026 01:58:36 +0000 (09:58 +0800)
committerluozheng <1732036298@qq.com>
Wed, 10 Jun 2026 01:58:36 +0000 (09:58 +0800)
code/sdk/zhihui/services/ble_service/blemoder/bleInstruct.cpp
code/sdk/zhihui/services/ble_service/blemoder/bleInstruct.h

index 2f4eb8b..ff755ca 100644 (file)
@@ -522,12 +522,18 @@ void delete_before_file(uint8_t type)
 
 }
 
-char * ble_sywee_history(uint8_t day,uint16_t *len ,uint8_t type)
+//同步历史数据
+char * ble_sywee_history(uint8_t day,uint32_t *len ,uint8_t type)
 {
 #ifdef SUPPORT_CYWEE_SERVICE
-   char path_[64]={0};
+    if (len == NULL) {
+        return NULL;
+    }
+    *len = 0;
+
+    char path_[64]={0};
     char timeday[12] = {0};
-    uapi_get_YYMMDD_Time_extend(timeday, '_',day);
+    uapi_get_YYMMDD_Time_extend(timeday, '_', (int8_t)day);
     uint8_t day_now_data_size=0;
     switch (type)
     {
@@ -552,54 +558,57 @@ char * ble_sywee_history(uint8_t day,uint16_t *len ,uint8_t type)
         day_now_data_size=8;
             break;
         default:
-            break;
+            return NULL;
     }
     if(day!=0)
     {
         day_now_data_size=0;
     }
-    FILE * fp = fopen(path_,"rb+");
+    FILE * fp = fopen(path_,"rb");
     uint32_t size=0;
     if(fp==NULL)
     {
-        
-        WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP," fopen type  %d %s fail\r\n",type,path_);
         if(day==0)
         {
             goto DAY_IS_0;
         }
-          WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP," fopen type  %d %s fail\r\n",type,path_);
         return NULL;
     }
-   
-    if (fp != NULL)
-    {
-        fseek(fp, 0, SEEK_END); // 将文件指针移动到文件末尾
-        size = ftell(fp);       // 获取当前文件指针位置,即文件大小
-        fseek(fp, 0, SEEK_SET); // 将文件指针移回文件开头
-    }
-   
+
+    fseek(fp, 0, SEEK_END);
+    size = (uint32_t)ftell(fp);
+    fseek(fp, 0, SEEK_SET);
 
     DAY_IS_0:
 
-    //  WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP,"size %d \r\n",size);
     char * buf=(char *)malloc(size+day_now_data_size);
     if(buf==NULL)
     {
-          WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP,"malloc fail\r\n");
-        return buf;
+        if (fp != NULL) {
+            fclose(fp);
+        }
+        return NULL;
     }
     if(fp)
     {
-        uint32_t ret= fread(buf,1,size,fp);
+        uint32_t read_size = fread(buf, 1, size, fp);
         fclose(fp);
+        if (read_size != size) {
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"fread type %d %s fail, expect %u got %u\r\n",
+                type, path_, size, read_size);
+            if (day != 0) {
+                free(buf);
+                return NULL;
+            }
+            size = 0;
+        }
     }
-    
+
     if(day==0)
     {
         time_t currtime;
         uint32_t creationTime=0;
-        int ret = uapi_get_timestampe_ss(&creationTime);
+        uapi_get_timestampe_ss(&creationTime);
 
         int32_t value=0;
         int16_t value_16=0;
@@ -608,9 +617,12 @@ char * ble_sywee_history(uint8_t day,uint16_t *len ,uint8_t type)
         struct tm *timeinfo;
         time(&currtime);
         timeinfo = localtime(&currtime);
-          WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP,"localtime %d",timeinfo->tm_min);
+        if (timeinfo == NULL) {
+            free(buf);
+            return NULL;
+        }
         creationTime=creationTime-(timeinfo->tm_min*60)-(timeinfo->tm_sec);
-       
+
         switch (type)
         {
             case 0x43: //同步步数历史数据
@@ -625,7 +637,7 @@ char * ble_sywee_history(uint8_t day,uint16_t *len ,uint8_t type)
                         size+=4;
                     }
                 }
-              
+
                 break;
             case 0x44://同步燃脂时间数据
                 if(cywee_get_now_hour_data(&value_16,FAT))
@@ -639,20 +651,20 @@ char * ble_sywee_history(uint8_t day,uint16_t *len ,uint8_t type)
                         size+=2;
                     }
                 }
-               
+
                 break;
             case 0x45://同步站立历史数据
-
+                
                 // if(cywee_get_stand_flag())
                 // {
                 //     memcpy(buf+size,&creationTime,4);
-                    //     size+=4;
+                //     size+=4;
                 // }
-               
+
                 break;
             case 0x46://同步卡路里历史数据
                 if(cywee_get_now_hour_data(&f_value,CALORIES))
-                {   
+                {
                     if(f_value>0)
                     {
                         memcpy(buf+size,&creationTime,4);
@@ -661,7 +673,7 @@ char * ble_sywee_history(uint8_t day,uint16_t *len ,uint8_t type)
                         size+=4;
                     }
                 }
-              
+
                 break;
             case 0x47://同步距离历史数据
                 if(cywee_get_now_hour_data(&f_value,DISTANCE))
@@ -674,7 +686,7 @@ char * ble_sywee_history(uint8_t day,uint16_t *len ,uint8_t type)
                         size+=4;
                     }
                 }
-               
+
                 break;
             default:
                 break;
@@ -682,21 +694,20 @@ char * ble_sywee_history(uint8_t day,uint16_t *len ,uint8_t type)
 
     }
 
-    //   WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP,"fread %d \r\n",ret);
-    for(int i=0;i<size;i++)
-    {
-          WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP," %02X",(uint8_t)buf[i]);
-    }
     *len=size;
-    if(buf && size==0)
+    if(size==0)
     {
         free(buf);
         buf=NULL;
     }
-   
+
     return buf;
-    #endif
+#else
+    (void)day;
+    (void)len;
+    (void)type;
     return NULL;
+#endif
 
 }
 
index e05c83d..e0084e5 100644 (file)
@@ -205,7 +205,7 @@ void ble_get_sleep_remind(msg_get_sleep_param_info_t *data);
 
 void ble_set_sleep_remind(msg_get_sleep_param_info_t data);
 
-char * ble_sywee_history(uint8_t day,uint16_t *len,uint8_t type);
+char * ble_sywee_history(uint8_t day,uint32_t *len,uint8_t type);