Commit fd1c55cc authored by ddkilzer@apple.com's avatar ddkilzer@apple.com

Fix DateMath.cpp to compile with -Wshorten-64-to-32

<http://webkit.org/b/107503>

Reviewed by Darin Adler.

Source/JavaScriptCore:

* 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.


git-svn-id: http://svn.webkit.org/repository/webkit/trunk@140437 268f45cc-cd09-0410-ab3c-d52691b4dbfc
parent cad119de
2013-01-22 David Kilzer <ddkilzer@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_horton@apple.com>
PDFPlugin: Build PDFPlugin everywhere, enable at runtime
......
......@@ -254,7 +254,7 @@ double parseDateFromNullTerminatedCharacters(ExecState* exec, const char* dateSt
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);
}
......
2013-01-22 David Kilzer <ddkilzer@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 <aestes@apple.com>
Add a USE() macro for content filtering code
......
......@@ -485,7 +485,7 @@ void initializeDates()
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 @@ static int findMonth(const char* monthStr)
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 @@ static bool parseLong(const char* string, char** stopPosition, int base, long* r
// 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 @@ double parseES5DateFromNullTerminatedCharacters(const char* dateString)
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 @@ double parseDateFromNullTerminatedCharacters(const char* dateString, bool& haveT
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 @@ double parseDateFromNullTerminatedCharacters(const char* dateString, bool& haveT
// 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 @@ double parseDateFromNullTerminatedCharacters(const char* dateString, bool& haveT
// '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 @@ double parseDateFromNullTerminatedCharacters(const char* dateString, bool& haveT
// 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 @@ double parseDateFromNullTerminatedCharacters(const char* dateString, bool& haveT
}
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 @@ double parseDateFromNullTerminatedCharacters(const char* dateString, bool& haveT
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 @@ double parseDateFromNullTerminatedCharacters(const char* dateString, bool& haveT
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 @@ double parseDateFromNullTerminatedCharacters(const char* dateString, bool& haveT
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 @@ double parseDateFromNullTerminatedCharacters(const char* dateString)
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);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment