Donny9 commented on code in PR #6783:
URL: https://github.com/apache/incubator-nuttx/pull/6783#discussion_r998796561


##########
libs/libc/time/lib_localtime.c:
##########
@@ -611,168 +700,229 @@ static int tzload(FAR const char *name,
     }
 
   nread = _NX_READ(fid, up->buf, sizeof up->buf);
-  if (_NX_CLOSE(fid) < 0 || nread <= 0)
+  if (_NX_CLOSE(fid) < 0 || nread < tzheadsize)
     {
       goto oops;
     }
 
   for (stored = 4; stored <= 8; stored *= 2)
     {
-      int ttisstdcnt;
-      int ttisgmtcnt;
-      int timecnt;
-
-      ttisstdcnt  = (int)detzcode(up->tzhead.tzh_ttisstdcnt);
-      ttisgmtcnt  = (int)detzcode(up->tzhead.tzh_ttisgmtcnt);
-      sp->leapcnt = (int)detzcode(up->tzhead.tzh_leapcnt);
-      sp->timecnt = (int)detzcode(up->tzhead.tzh_timecnt);
-      sp->typecnt = (int)detzcode(up->tzhead.tzh_typecnt);
-      sp->charcnt = (int)detzcode(up->tzhead.tzh_charcnt);
-
-      p = up->tzhead.tzh_charcnt + sizeof up->tzhead.tzh_charcnt;
-      if (sp->leapcnt < 0 || sp->leapcnt > TZ_MAX_LEAPS ||
-          sp->typecnt <= 0 || sp->typecnt > TZ_MAX_TYPES ||
-          sp->timecnt < 0 || sp->timecnt > TZ_MAX_TIMES ||
-          sp->charcnt < 0 || sp->charcnt > TZ_MAX_CHARS ||
-          (ttisstdcnt != sp->typecnt && ttisstdcnt != 0) ||
-          (ttisgmtcnt != sp->typecnt && ttisgmtcnt != 0))
+      char version = up->tzhead.tzh_version[0];
+      int skip_datablock = stored == 4 && version;
+      int_fast32_t datablock_size;
+      int_fast32_t ttisstdcnt;
+      int_fast32_t ttisutcnt;
+      int_fast32_t leapcnt;
+      int_fast32_t timecnt;
+      int_fast32_t typecnt;
+      int_fast32_t charcnt;
+      int_fast64_t prevtr = -1;
+      int_fast32_t prevcorr;
+      FAR const char *p;
+
+      ttisstdcnt  = detzcode(up->tzhead.tzh_ttisstdcnt);
+      ttisutcnt  = detzcode(up->tzhead.tzh_ttisutcnt);
+      leapcnt = detzcode(up->tzhead.tzh_leapcnt);
+      timecnt = detzcode(up->tzhead.tzh_timecnt);
+      typecnt = detzcode(up->tzhead.tzh_typecnt);
+      charcnt = detzcode(up->tzhead.tzh_charcnt);
+      p = up->buf + tzheadsize;
+      if (leapcnt < 0 || leapcnt > TZ_MAX_LEAPS ||
+          typecnt < 0 || typecnt > TZ_MAX_TYPES ||
+          timecnt < 0 || timecnt > TZ_MAX_TIMES ||
+          charcnt < 0 || charcnt > TZ_MAX_CHARS ||
+          ttisstdcnt < 0 || ttisstdcnt > TZ_MAX_TYPES ||
+          ttisutcnt < 0 || ttisutcnt > TZ_MAX_TYPES)
         {
           goto oops;
         }
 
-      if (nread - (p - up->buf) < sp->timecnt * stored + /* ats */
-          sp->timecnt +                                  /* types */
-          sp->typecnt * 6 +                              /* ttinfos */
-          sp->charcnt +                                  /* chars */
-          sp->leapcnt * (stored + 4) +                   /* lsinfos */
-          ttisstdcnt +                                   /* ttisstds */
-          ttisgmtcnt)                                    /* ttisgmts */
+      datablock_size = (timecnt * stored          /* ats */
+                     + timecnt                    /* types */
+                     + typecnt * 6                /* ttinfos */
+                     + charcnt                    /* chars */
+                     + leapcnt * (stored + 4)     /* lsinfos */
+                     + ttisstdcnt                 /* ttisstds */
+                     + ttisutcnt);                /* ttisuts */
+      if (nread - tzheadsize < datablock_size)
         {
           goto oops;
         }
 
-      timecnt = 0;
-      for (i = 0; i < sp->timecnt; ++i)
+      if (skip_datablock)
         {
-          int_fast64_t at = stored == 4 ? detzcode(p) : detzcode64(p);
-          sp->types[i] = ((TYPE_SIGNED(time_t)
-                           ? g_min_timet <= at : 0 <= at) &&
-                           at <= g_max_timet);
-          if (sp->types[i])
+          p += datablock_size;
+        }
+      else
+        {
+          if (!((ttisstdcnt == typecnt || ttisstdcnt == 0) &&
+              (ttisutcnt == typecnt || ttisutcnt == 0)))
+            {
+              goto oops;
+            }
+
+          sp->leapcnt = leapcnt;
+          sp->timecnt = timecnt;
+          sp->typecnt = typecnt;
+          sp->charcnt = charcnt;
+
+          timecnt = 0;
+          for (i = 0; i < sp->timecnt; ++i)
             {
-              if (i && !timecnt && at != g_min_timet)
+              int_fast64_t at = stored == 4 ? detzcode(p) : detzcode64(p);
+              sp->types[i] = at <= TIME_T_MAX;
+              if (sp->types[i])
                 {
-                  /* Keep the earlier record, but tweak
-                   * it so that it starts with the
-                   * minimum time_t value.
-                   */
+                  time_t attime = ((TYPE_SIGNED(time_t) ?
+                                  at < TIME_T_MIN : at < 0) ?
+                                  TIME_T_MIN : at);
+                  if (timecnt && attime <= sp->ats[timecnt - 1])
+                    {
+                      if (attime < sp->ats[timecnt - 1])
+                        {
+                          goto oops;
+                        }
+
+                      sp->types[i - 1] = 0;
+                      timecnt--;
+                    }
 
-                  sp->types[i - 1] = 1;
-                  sp->ats[timecnt++] = g_min_timet;
+                  sp->ats[timecnt++] = attime;
                 }
 
-              sp->ats[timecnt++] = at;
+              p += stored;
             }
 
-          p += stored;
-        }
-
-      timecnt = 0;
-      for (i = 0; i < sp->timecnt; ++i)
-        {
-          unsigned char typ = *p++;
-          if (sp->typecnt <= typ)
+          timecnt = 0;
+          for (i = 0; i < sp->timecnt; ++i)
             {
-              goto oops;
+              unsigned char typ = *p++;

Review Comment:
   Don't need?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to