增加goalmodel的安全性 3296/1 3296/head 3298/target/1
authorCaisking <Casiking@zhihuishixun.com>
Thu, 19 Mar 2026 10:27:12 +0000 (18:27 +0800)
committerCaisking <Casiking@zhihuishixun.com>
Thu, 19 Mar 2026 10:27:12 +0000 (18:27 +0800)
code/sdk/application/wearable/fd272/nativeapp/nativeui/goal/src/goalmodel.cpp

index 9eb81bb..4e7c0e0 100644 (file)
@@ -66,6 +66,10 @@ namespace OHOS
    uint8_t GoalModel::getstepvalue()
    {
       int stepgoal = step_goal;
+      if (stepgoal <= 0)
+      {
+         return 0;
+      }
       int stepdata = show_data_step_refresh();//cywee_get_step_data();//OHOS::ActivityModel::GetInstance()->GetStepCounter();
       // WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "%d %d %f ",stepdata,stepgoal,((float)stepdata/(float)stepgoal) * 100);
       uint8_t value = ((float)stepdata / (float)stepgoal) * 100;
@@ -325,11 +329,24 @@ namespace OHOS
       {
          return 0;
       }
+      if (size == 0 || (size % 8) != 0)
+      {
+         fclose(fp);
+         return 0;
+      }
       uint8_t *date=(uint8_t *)malloc(size);
-      if(date==NULL) return 0;
+      if(date==NULL) {
+         fclose(fp);
+         return 0;
+      }
       
-      fread(date,sizeof(uint8_t),size,fp);
+      size_t readSize = fread(date,sizeof(uint8_t),size,fp);
       fclose(fp);
+      if (readSize != size)
+      {
+         free(date);
+         return 0;
+      }
       int32_t sing_date=0;
       int32_t timetemp=0;
       
@@ -341,8 +358,12 @@ namespace OHOS
          WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "time: %d", timetemp);
          WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "data: %d", sing_date);
 
-         struct tm *timeinfo;
-         timeinfo = localtime((time_t *) &timetemp);
+         time_t ts = static_cast<time_t>(timetemp);
+         struct tm *timeinfo = localtime(&ts);
+         if (timeinfo == nullptr)
+         {
+            continue;
+         }
 
    
          if(sing_date>0 && timeinfo->tm_mday==day_now && timeinfo->tm_mon==mon_now)
@@ -384,11 +405,24 @@ namespace OHOS
       {
          return 0;
       }
+      if (size == 0 || (size % 8) != 0)
+      {
+         fclose(fp);
+         return 0;
+      }
       uint8_t *date=(uint8_t *)malloc(size);
-      if(date==NULL) return 0;
+      if(date==NULL) {
+         fclose(fp);
+         return 0;
+      }
       
-      fread(date,sizeof(uint8_t),size,fp);
+      size_t readSize = fread(date,sizeof(uint8_t),size,fp);
       fclose(fp);
+      if (readSize != size)
+      {
+         free(date);
+         return 0;
+      }
       float sing_date=0;
       int32_t timetemp=0;
       
@@ -401,8 +435,12 @@ namespace OHOS
          WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "data: %f", sing_date);
 
 
-         struct tm *timeinfo;
-         timeinfo = localtime((time_t *) &timetemp);
+         time_t ts = static_cast<time_t>(timetemp);
+         struct tm *timeinfo = localtime(&ts);
+         if (timeinfo == nullptr)
+         {
+            continue;
+         }
 
    
          if(sing_date>0 && timeinfo->tm_mday==day_now && timeinfo->tm_mon==mon_now)
@@ -445,11 +483,24 @@ namespace OHOS
       {
          return 0;
       }
+      if (size == 0 || (size % 8) != 0)
+      {
+         fclose(fp);
+         return 0;
+      }
       uint8_t *date=(uint8_t *)malloc(size);
-      if(date==NULL) return 0;
+      if(date==NULL) {
+         fclose(fp);
+         return 0;
+      }
       
-      fread(date,sizeof(uint8_t),size,fp);
+      size_t readSize = fread(date,sizeof(uint8_t),size,fp);
       fclose(fp);
+      if (readSize != size)
+      {
+         free(date);
+         return 0;
+      }
       float sing_date=0;
       int32_t timetemp=0;
       
@@ -461,8 +512,12 @@ namespace OHOS
          WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "time: %d", timetemp);
          WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "data: %f", sing_date);
 
-         struct tm *timeinfo;
-         timeinfo = localtime((time_t *) &timetemp);
+         time_t ts = static_cast<time_t>(timetemp);
+         struct tm *timeinfo = localtime(&ts);
+         if (timeinfo == nullptr)
+         {
+            continue;
+         }
 
    
          if(sing_date>0 && timeinfo->tm_mday==day_now && timeinfo->tm_mon==mon_now)
@@ -506,11 +561,24 @@ namespace OHOS
       {
          return 0;
       }
+      if (size == 0 || (size % 6) != 0)
+      {
+         fclose(fp);
+         return 0;
+      }
       uint8_t *date=(uint8_t *)malloc(size);
-      if(date==NULL) return 0;
+      if(date==NULL) {
+         fclose(fp);
+         return 0;
+      }
       
-      fread(date,sizeof(uint8_t),size,fp);
+      size_t readSize = fread(date,sizeof(uint8_t),size,fp);
       fclose(fp);
+      if (readSize != size)
+      {
+         free(date);
+         return 0;
+      }
       uint16 sing_date=0;
       int32_t timetemp=0;
       
@@ -522,8 +590,12 @@ namespace OHOS
          WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "time: %d", timetemp);
          WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "data: %d", sing_date);
 
-         struct tm *timeinfo;
-         timeinfo = localtime((time_t *) &timetemp);
+         time_t ts = static_cast<time_t>(timetemp);
+         struct tm *timeinfo = localtime(&ts);
+         if (timeinfo == nullptr)
+         {
+            continue;
+         }
 
    
          if(sing_date>0 && timeinfo->tm_mday==day_now && timeinfo->tm_mon==mon_now)