Title: [140437] trunk/Source
Revision
140437
Author
ddkil...@apple.com
Date
2013-01-22 10:27:23 -0800 (Tue, 22 Jan 2013)

Log Message

Fix DateMath.cpp to compile with -Wshorten-64-to-32
<http://webkit.org/b/107503>

Reviewed by Darin Adler.

Source/_javascript_Core:

* runtime/JSDateMath.cpp:
(JSC::parseDateFromNullTerminatedCharacters): Remove unneeded
static_cast<int>().

Source/WTF:

Fixes the following build errors with -Wshorten-64-to-32:

    DateMath.cpp:742:47: error: implicit conversion loses integer precision: 'long' to 'int' [-Werror,-Wshorten-64-to-32]
        if (month == 2 && day > 28 && !isLeapYear(year))
                                       ~~~~~~~~~~ ^~~~
    DateMath.cpp:757:48: error: implicit conversion loses integer precision: 'long' to 'int' [-Werror,-Wshorten-64-to-32]
        double dateSeconds = ymdhmsToSeconds(year, month, day, hours, minutes, seconds) - timeZoneSeconds;
                             ~~~~~~~~~~~~~~~       ^~~~~
    DateMath.cpp:757:55: error: implicit conversion loses integer precision: 'long' to 'int' [-Werror,-Wshorten-64-to-32]
        double dateSeconds = ymdhmsToSeconds(year, month, day, hours, minutes, seconds) - timeZoneSeconds;
                             ~~~~~~~~~~~~~~~              ^~~
    DateMath.cpp:757:60: error: implicit conversion loses integer precision: 'long' to 'int' [-Werror,-Wshorten-64-to-32]
        double dateSeconds = ymdhmsToSeconds(year, month, day, hours, minutes, seconds) - timeZoneSeconds;
                             ~~~~~~~~~~~~~~~                   ^~~~~
    DateMath.cpp:757:67: error: implicit conversion loses integer precision: 'long' to 'int' [-Werror,-Wshorten-64-to-32]
        double dateSeconds = ymdhmsToSeconds(year, month, day, hours, minutes, seconds) - timeZoneSeconds;
                             ~~~~~~~~~~~~~~~                          ^~~~~~~
    DateMath.cpp:996:59: error: implicit conversion loses integer precision: 'long' to 'int' [-Werror,-Wshorten-64-to-32]
                        offset = ((o / 100) * 60 + (o % 100)) * sgn;
                               ~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~
    DateMath.cpp:998:37: error: implicit conversion loses integer precision: 'long' to 'int' [-Werror,-Wshorten-64-to-32]
                        offset = o * 60 * sgn;
                               ~ ~~~~~~~^~~~~
    DateMath.cpp:1005:40: error: implicit conversion loses integer precision: 'long' to 'int' [-Werror,-Wshorten-64-to-32]
                    offset = (o * 60 + o2) * sgn;
                           ~ ~~~~~~~~~~~~~~^~~~~
    DateMath.cpp:1041:40: error: implicit conversion loses integer precision: 'long' to 'int' [-Werror,-Wshorten-64-to-32]
        return ymdhmsToSeconds(year, month + 1, day, hour, minute, second) * msPerSecond;
               ~~~~~~~~~~~~~~~       ~~~~~~^~~
    DateMath.cpp:1041:45: error: implicit conversion loses integer precision: 'long' to 'int' [-Werror,-Wshorten-64-to-32]
        return ymdhmsToSeconds(year, month + 1, day, hour, minute, second) * msPerSecond;
               ~~~~~~~~~~~~~~~                  ^~~
    DateMath.cpp:1041:50: error: implicit conversion loses integer precision: 'long' to 'int' [-Werror,-Wshorten-64-to-32]
        return ymdhmsToSeconds(year, month + 1, day, hour, minute, second) * msPerSecond;
               ~~~~~~~~~~~~~~~                       ^~~~
    DateMath.cpp:1041:56: error: implicit conversion loses integer precision: 'long' to 'int' [-Werror,-Wshorten-64-to-32]
        return ymdhmsToSeconds(year, month + 1, day, hour, minute, second) * msPerSecond;
               ~~~~~~~~~~~~~~~                             ^~~~~~
    12 errors generated.

* wtf/DateMath.cpp:
(WTF::ymdhmsToSeconds): Change year argument from long to int.
Change mon, day, hour, minute arguments from int to long.
(WTF::parseInt): Add.  Identical to parseLong but bounds checks
for type int.
(WTF::parseLong): Switch to std::numeric_limits<long> instead of
macros.
(WTF::parseES5DatePortion): Change year argument from long to
int.
(WTF::parseES5DateFromNullTerminatedCharacters): Change year
local variable from long to int.
(WTF::parseDateFromNullTerminatedCharacters): Change year and
offset local variables from long to int.  Switch from using
parseLong() to parseInt() as needed.  Ditto for labs() to abs().
Add overflow check when switching from "MM/DD/YYYY" to
"YYYY/MM/DD" parsing.

Modified Paths

Diff

Modified: trunk/Source/_javascript_Core/ChangeLog (140436 => 140437)


--- trunk/Source/_javascript_Core/ChangeLog	2013-01-22 18:22:01 UTC (rev 140436)
+++ trunk/Source/_javascript_Core/ChangeLog	2013-01-22 18:27:23 UTC (rev 140437)
@@ -1,3 +1,14 @@
+2013-01-22  David Kilzer  <ddkil...@apple.com>
+
+        Fix DateMath.cpp to compile with -Wshorten-64-to-32
+        <http://webkit.org/b/107503>
+
+        Reviewed by Darin Adler.
+
+        * runtime/JSDateMath.cpp:
+        (JSC::parseDateFromNullTerminatedCharacters): Remove unneeded
+        static_cast<int>().
+
 2013-01-22  Tim Horton  <timothy_hor...@apple.com>
 
         PDFPlugin: Build PDFPlugin everywhere, enable at runtime

Modified: trunk/Source/_javascript_Core/runtime/JSDateMath.cpp (140436 => 140437)


--- trunk/Source/_javascript_Core/runtime/JSDateMath.cpp	2013-01-22 18:22:01 UTC (rev 140436)
+++ trunk/Source/_javascript_Core/runtime/JSDateMath.cpp	2013-01-22 18:27:23 UTC (rev 140437)
@@ -254,7 +254,7 @@
     if (!haveTZ) {
         double utcOffset = getUTCOffset(exec);
         double dstOffset = getDSTOffset(exec, ms, utcOffset);
-        offset = static_cast<int>((utcOffset + dstOffset) / WTF::msPerMinute);
+        offset = (utcOffset + dstOffset) / WTF::msPerMinute;
     }
     return ms - (offset * WTF::msPerMinute);
 }

Modified: trunk/Source/WTF/ChangeLog (140436 => 140437)


--- trunk/Source/WTF/ChangeLog	2013-01-22 18:22:01 UTC (rev 140436)
+++ trunk/Source/WTF/ChangeLog	2013-01-22 18:27:23 UTC (rev 140437)
@@ -1,3 +1,67 @@
+2013-01-22  David Kilzer  <ddkil...@apple.com>
+
+        Fix DateMath.cpp to compile with -Wshorten-64-to-32
+        <http://webkit.org/b/107503>
+
+        Reviewed by Darin Adler.
+
+        Fixes the following build errors with -Wshorten-64-to-32:
+
+            DateMath.cpp:742:47: error: implicit conversion loses integer precision: 'long' to 'int' [-Werror,-Wshorten-64-to-32]
+                if (month == 2 && day > 28 && !isLeapYear(year))
+                                               ~~~~~~~~~~ ^~~~
+            DateMath.cpp:757:48: error: implicit conversion loses integer precision: 'long' to 'int' [-Werror,-Wshorten-64-to-32]
+                double dateSeconds = ymdhmsToSeconds(year, month, day, hours, minutes, seconds) - timeZoneSeconds;
+                                     ~~~~~~~~~~~~~~~       ^~~~~
+            DateMath.cpp:757:55: error: implicit conversion loses integer precision: 'long' to 'int' [-Werror,-Wshorten-64-to-32]
+                double dateSeconds = ymdhmsToSeconds(year, month, day, hours, minutes, seconds) - timeZoneSeconds;
+                                     ~~~~~~~~~~~~~~~              ^~~
+            DateMath.cpp:757:60: error: implicit conversion loses integer precision: 'long' to 'int' [-Werror,-Wshorten-64-to-32]
+                double dateSeconds = ymdhmsToSeconds(year, month, day, hours, minutes, seconds) - timeZoneSeconds;
+                                     ~~~~~~~~~~~~~~~                   ^~~~~
+            DateMath.cpp:757:67: error: implicit conversion loses integer precision: 'long' to 'int' [-Werror,-Wshorten-64-to-32]
+                double dateSeconds = ymdhmsToSeconds(year, month, day, hours, minutes, seconds) - timeZoneSeconds;
+                                     ~~~~~~~~~~~~~~~                          ^~~~~~~
+            DateMath.cpp:996:59: error: implicit conversion loses integer precision: 'long' to 'int' [-Werror,-Wshorten-64-to-32]
+                                offset = ((o / 100) * 60 + (o % 100)) * sgn;
+                                       ~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~
+            DateMath.cpp:998:37: error: implicit conversion loses integer precision: 'long' to 'int' [-Werror,-Wshorten-64-to-32]
+                                offset = o * 60 * sgn;
+                                       ~ ~~~~~~~^~~~~
+            DateMath.cpp:1005:40: error: implicit conversion loses integer precision: 'long' to 'int' [-Werror,-Wshorten-64-to-32]
+                            offset = (o * 60 + o2) * sgn;
+                                   ~ ~~~~~~~~~~~~~~^~~~~
+            DateMath.cpp:1041:40: error: implicit conversion loses integer precision: 'long' to 'int' [-Werror,-Wshorten-64-to-32]
+                return ymdhmsToSeconds(year, month + 1, day, hour, minute, second) * msPerSecond;
+                       ~~~~~~~~~~~~~~~       ~~~~~~^~~
+            DateMath.cpp:1041:45: error: implicit conversion loses integer precision: 'long' to 'int' [-Werror,-Wshorten-64-to-32]
+                return ymdhmsToSeconds(year, month + 1, day, hour, minute, second) * msPerSecond;
+                       ~~~~~~~~~~~~~~~                  ^~~
+            DateMath.cpp:1041:50: error: implicit conversion loses integer precision: 'long' to 'int' [-Werror,-Wshorten-64-to-32]
+                return ymdhmsToSeconds(year, month + 1, day, hour, minute, second) * msPerSecond;
+                       ~~~~~~~~~~~~~~~                       ^~~~
+            DateMath.cpp:1041:56: error: implicit conversion loses integer precision: 'long' to 'int' [-Werror,-Wshorten-64-to-32]
+                return ymdhmsToSeconds(year, month + 1, day, hour, minute, second) * msPerSecond;
+                       ~~~~~~~~~~~~~~~                             ^~~~~~
+            12 errors generated.
+
+        * wtf/DateMath.cpp:
+        (WTF::ymdhmsToSeconds): Change year argument from long to int.
+        Change mon, day, hour, minute arguments from int to long.
+        (WTF::parseInt): Add.  Identical to parseLong but bounds checks
+        for type int.
+        (WTF::parseLong): Switch to std::numeric_limits<long> instead of
+        macros.
+        (WTF::parseES5DatePortion): Change year argument from long to
+        int.
+        (WTF::parseES5DateFromNullTerminatedCharacters): Change year
+        local variable from long to int.
+        (WTF::parseDateFromNullTerminatedCharacters): Change year and
+        offset local variables from long to int.  Switch from using
+        parseLong() to parseInt() as needed.  Ditto for labs() to abs().
+        Add overflow check when switching from "MM/DD/YYYY" to
+        "YYYY/MM/DD" parsing.
+
 2013-01-17  Andy Estes  <aes...@apple.com>
 
         Add a USE() macro for content filtering code

Modified: trunk/Source/WTF/wtf/DateMath.cpp (140436 => 140437)


--- trunk/Source/WTF/wtf/DateMath.cpp	2013-01-22 18:22:01 UTC (rev 140436)
+++ trunk/Source/WTF/wtf/DateMath.cpp	2013-01-22 18:27:23 UTC (rev 140437)
@@ -485,7 +485,7 @@
     equivalentYearForDST(2000); // Need to call once to initialize a static used in this function.
 }
 
-static inline double ymdhmsToSeconds(long year, int mon, int day, int hour, int minute, double second)
+static inline double ymdhmsToSeconds(int year, long mon, long day, long hour, long minute, double second)
 {
     double days = (day - 32075)
         + floor(1461 * (year + 4800.0 + (mon - 14) / 12) / 4)
@@ -554,11 +554,21 @@
     return -1;
 }
 
+static bool parseInt(const char* string, char** stopPosition, int base, int* result)
+{
+    long longResult = strtol(string, stopPosition, base);
+    // Avoid the use of errno as it is not available on Windows CE
+    if (string == *stopPosition || longResult <= std::numeric_limits<int>::min() || longResult >= std::numeric_limits<int>::max())
+        return false;
+    *result = static_cast<int>(longResult);
+    return true;
+}
+
 static bool parseLong(const char* string, char** stopPosition, int base, long* result)
 {
     *result = strtol(string, stopPosition, base);
     // Avoid the use of errno as it is not available on Windows CE
-    if (string == *stopPosition || *result == LONG_MIN || *result == LONG_MAX)
+    if (string == *stopPosition || *result == std::numeric_limits<long>::min() || *result == std::numeric_limits<long>::max())
         return false;
     return true;
 }
@@ -566,14 +576,14 @@
 // Parses a date with the format YYYY[-MM[-DD]].
 // Year parsing is lenient, allows any number of digits, and +/-.
 // Returns 0 if a parse error occurs, else returns the end of the parsed portion of the string.
-static char* parseES5DatePortion(const char* currentPosition, long& year, long& month, long& day)
+static char* parseES5DatePortion(const char* currentPosition, int& year, long& month, long& day)
 {
     char* postParsePosition;
 
     // This is a bit more lenient on the year string than ES5 specifies:
     // instead of restricting to 4 digits (or 6 digits with mandatory +/-),
     // it accepts any integer value. Consider this an implementation fallback.
-    if (!parseLong(currentPosition, &postParsePosition, 10, &year))
+    if (!parseInt(currentPosition, &postParsePosition, 10, &year))
         return 0;
 
     // Check for presence of -MM portion.
@@ -710,7 +720,7 @@
     static const long daysPerMonth[12] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
     
     // The year must be present, but the other fields may be omitted - see ES5.1 15.9.1.15.
-    long year = 0;
+    int year = 0;
     long month = 1;
     long day = 1;
     long hours = 0;
@@ -816,7 +826,7 @@
     if (day < 0)
         return std::numeric_limits<double>::quiet_NaN();
 
-    long year = 0;
+    int year = 0;
     if (day > 31) {
         // ### where is the boundary and what happens below?
         if (*dateString != '/')
@@ -824,7 +834,9 @@
         // looks like a YYYY/MM/DD date
         if (!*++dateString)
             return std::numeric_limits<double>::quiet_NaN();
-        year = day;
+        if (day <= std::numeric_limits<int>::min() || day >= std::numeric_limits<int>::max())
+            return std::numeric_limits<double>::quiet_NaN();
+        year = static_cast<int>(day);
         if (!parseLong(dateString, &newPosStr, 10, &month))
             return std::numeric_limits<double>::quiet_NaN();
         month -= 1;
@@ -879,7 +891,7 @@
 
     // '99 23:12:40 GMT'
     if (year <= 0 && *dateString) {
-        if (!parseLong(dateString, &newPosStr, 10, &year))
+        if (!parseInt(dateString, &newPosStr, 10, &year))
             return std::numeric_limits<double>::quiet_NaN();
     }
 
@@ -966,7 +978,7 @@
     
     // The year may be after the time but before the time zone.
     if (isASCIIDigit(*dateString) && year == -1) {
-        if (!parseLong(dateString, &newPosStr, 10, &year))
+        if (!parseInt(dateString, &newPosStr, 10, &year))
             return std::numeric_limits<double>::quiet_NaN();
         dateString = newPosStr;
         skipSpacesAndComments(dateString);
@@ -981,8 +993,8 @@
         }
 
         if (*dateString == '+' || *dateString == '-') {
-            long o;
-            if (!parseLong(dateString, &newPosStr, 10, &o))
+            int o;
+            if (!parseInt(dateString, &newPosStr, 10, &o))
                 return std::numeric_limits<double>::quiet_NaN();
             dateString = newPosStr;
 
@@ -990,7 +1002,7 @@
                 return std::numeric_limits<double>::quiet_NaN();
 
             int sgn = (o < 0) ? -1 : 1;
-            o = labs(o);
+            o = abs(o);
             if (*dateString != ':') {
                 if (o >= 24)
                     offset = ((o / 100) * 60 + (o % 100)) * sgn;
@@ -998,8 +1010,8 @@
                     offset = o * 60 * sgn;
             } else { // GMT+05:00
                 ++dateString; // skip the ':'
-                long o2;
-                if (!parseLong(dateString, &newPosStr, 10, &o2))
+                int o2;
+                if (!parseInt(dateString, &newPosStr, 10, &o2))
                     return std::numeric_limits<double>::quiet_NaN();
                 dateString = newPosStr;
                 offset = (o * 60 + o2) * sgn;
@@ -1020,7 +1032,7 @@
     skipSpacesAndComments(dateString);
 
     if (*dateString && year == -1) {
-        if (!parseLong(dateString, &newPosStr, 10, &year))
+        if (!parseInt(dateString, &newPosStr, 10, &year))
             return std::numeric_limits<double>::quiet_NaN();
         dateString = newPosStr;
         skipSpacesAndComments(dateString);
@@ -1053,7 +1065,7 @@
     if (!haveTZ) {
         double utcOffset = calculateUTCOffset();
         double dstOffset = calculateDSTOffset(ms, utcOffset);
-        offset = static_cast<int>((utcOffset + dstOffset) / msPerMinute);
+        offset = (utcOffset + dstOffset) / msPerMinute;
     }
     return ms - (offset * msPerMinute);
 }
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
http://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to