]> granicus.if.org Git - php/commitdiff
Elevate warnings to ValueErrors in ext/calendar
authorChristoph M. Becker <cmbecker69@gmx.de>
Mon, 28 Oct 2019 07:53:56 +0000 (08:53 +0100)
committerChristoph M. Becker <cmbecker69@gmx.de>
Thu, 31 Oct 2019 07:49:15 +0000 (08:49 +0100)
All of these warnings/ValueErrors are due to programming errors, i.e.
calling a function with unsupported arguments.

13 files changed:
ext/calendar/cal_unix.c
ext/calendar/calendar.c
ext/calendar/calendar.stub.php
ext/calendar/calendar_arginfo.h
ext/calendar/easter.c
ext/calendar/tests/cal_days_in_month_error1.phpt
ext/calendar/tests/cal_from_jd_error1.phpt
ext/calendar/tests/cal_info.phpt
ext/calendar/tests/cal_to_jd_error1.phpt
ext/calendar/tests/easter_date.phpt
ext/calendar/tests/jdtojewish.phpt
ext/calendar/tests/jdtounix_error1.phpt
ext/calendar/tests/unixtojd_error1.phpt

index e2c7cf064a466a25a316877fd4c61339f2f1c621..78e69522681548d4452521c49e31a39e8443df8c 100644 (file)
@@ -35,7 +35,8 @@ PHP_FUNCTION(unixtojd)
        if (!ts) {
                ts = time(NULL);
        } else if (ts < 0) {
-               RETURN_FALSE;
+               zend_value_error("timestamp must not be negative");
+               return;
        }
 
        if (!(ta = php_localtime_r(&ts, &tmbuf))) {
@@ -57,8 +58,9 @@ PHP_FUNCTION(jdtounix)
        }
        uday -= 2440588 /* J.D. of 1.1.1970 */;
 
-       if (uday < 0 || uday > 24755) { /* before beginning of unix epoch or behind end of unix epoch */
-               RETURN_FALSE;
+       if (uday < 0 || uday > 24755) {
+               zend_value_error("jday must be within the Unix epoch");
+               return;
        }
 
        RETURN_LONG(uday * 24 * 3600);
index e450722dd0a6fd47c982ace6508bc9e02e13208a..9acdbc00f9dbc62b9d8235bc39926b2edd705bac 100644 (file)
@@ -218,8 +218,8 @@ PHP_FUNCTION(cal_info)
 
 
        if (cal != -1 && (cal < 0 || cal >= CAL_NUM_CALS)) {
-               php_error_docref(NULL, E_WARNING, "invalid calendar ID " ZEND_LONG_FMT, cal);
-               RETURN_FALSE;
+               zend_value_error("invalid calendar ID " ZEND_LONG_FMT, cal);
+               return;
        }
 
        _php_cal_info(cal, return_value);
@@ -240,8 +240,8 @@ PHP_FUNCTION(cal_days_in_month)
        }
 
        if (cal < 0 || cal >= CAL_NUM_CALS) {
-               php_error_docref(NULL, E_WARNING, "invalid calendar ID " ZEND_LONG_FMT, cal);
-               RETURN_FALSE;
+               zend_value_error("invalid calendar ID " ZEND_LONG_FMT, cal);
+               return;
        }
 
        calendar = &cal_conversion_table[cal];
@@ -249,8 +249,8 @@ PHP_FUNCTION(cal_days_in_month)
        sdn_start = calendar->to_jd(year, month, 1);
 
        if (sdn_start == 0) {
-               php_error_docref(NULL, E_WARNING, "invalid date");
-               RETURN_FALSE;
+               zend_value_error("invalid date");
+               return;
        }
 
        sdn_next = calendar->to_jd(year, 1 + month, 1);
@@ -286,8 +286,8 @@ PHP_FUNCTION(cal_to_jd)
        }
 
        if (cal < 0 || cal >= CAL_NUM_CALS) {
-               php_error_docref(NULL, E_WARNING, "invalid calendar ID " ZEND_LONG_FMT, cal);
-               RETURN_FALSE;
+               zend_value_error("invalid calendar ID " ZEND_LONG_FMT, cal);
+               return;
        }
 
        RETURN_LONG(cal_conversion_table[cal].to_jd(year, month, day));
@@ -307,8 +307,8 @@ PHP_FUNCTION(cal_from_jd)
        }
 
        if (cal < 0 || cal >= CAL_NUM_CALS) {
-               php_error_docref(NULL, E_WARNING, "invalid calendar ID " ZEND_LONG_FMT, cal);
-               RETURN_FALSE;
+               zend_value_error("invalid calendar ID " ZEND_LONG_FMT, cal);
+               return;
        }
        calendar = &cal_conversion_table[cal];
 
@@ -522,8 +522,8 @@ PHP_FUNCTION(jdtojewish)
                RETURN_NEW_STR(zend_strpprintf(0, "%i/%i/%i", month, day, year));
        } else {
                if (year <= 0 || year > 9999) {
-                       php_error_docref(NULL, E_WARNING, "Year out of range (0-9999)");
-                       RETURN_FALSE;
+                       zend_value_error("Year out of range (0-9999)");
+                       return;
                }
 
                RETVAL_NEW_STR(zend_strpprintf(0, "%s %s %s", heb_number_to_chars(day, fl, &dayp), JEWISH_HEB_MONTH_NAME(year)[month], heb_number_to_chars(year, fl, &yearp)));
index 6731cd5c0ba3e4633e7aec9d09f839ca992b4077..888986a70827cf9f1d7564daa8beb0380f83ff9d 100644 (file)
@@ -1,19 +1,14 @@
 <?php
 
-/** @return int|false */
-function cal_days_in_month(int $calendar, int $month, int $year) {}
+function cal_days_in_month(int $calendar, int $month, int $year): int {}
 
-/** @return array|false */
-function cal_from_jd(int $jd, int $calendar) {}
+function cal_from_jd(int $jd, int $calendar): array {}
 
-/** @return array|false */
-function cal_info(?int $calendar = UNKNOWN) {}
+function cal_info(?int $calendar = UNKNOWN): array {}
 
-/** @return int|false */
-function cal_to_jd(int $calendar, int $month, int $day, int $year) {}
+function cal_to_jd(int $calendar, int $month, int $day, int $year): int {}
 
-/** @return int|false */
-function easter_date(int $year = UNKNOWN, int $method = CAL_EASTER_DEFAULT) {}
+function easter_date(int $year = UNKNOWN, int $method = CAL_EASTER_DEFAULT): int {}
 
 function easter_days(int $year = UNKNOWN, int $method = CAL_EASTER_DEFAULT): int {}
 
@@ -30,13 +25,11 @@ function jdtofrench(int $juliandaycount): string {}
 
 function jdtogregorian(int $juliandaycount): string {}
 
-/** @return string|false */
-function jdtojewish(int $juliandaycount, bool $hebrew = false, int $fl = 0) {}
+function jdtojewish(int $juliandaycount, bool $hebrew = false, int $fl = 0): string {}
 
 function jdtojulian(int $juliandaycount): string {}
 
-/** @return int|false */
-function jdtounix(int $jday) {}
+function jdtounix(int $jday): int {}
 
 function jewishtojd(int $month, int $day, int $year): int {}
 
index 6e526062b06156bd65df8ce342cafa7570bb53b6..a03ef6778bc4cef7357127a6b8f3586ed7176a53 100644 (file)
@@ -1,36 +1,33 @@
 /* This is a generated file, edit the .stub.php file instead. */
 
-ZEND_BEGIN_ARG_INFO_EX(arginfo_cal_days_in_month, 0, 0, 3)
+ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_cal_days_in_month, 0, 3, IS_LONG, 0)
        ZEND_ARG_TYPE_INFO(0, calendar, IS_LONG, 0)
        ZEND_ARG_TYPE_INFO(0, month, IS_LONG, 0)
        ZEND_ARG_TYPE_INFO(0, year, IS_LONG, 0)
 ZEND_END_ARG_INFO()
 
-ZEND_BEGIN_ARG_INFO_EX(arginfo_cal_from_jd, 0, 0, 2)
+ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_cal_from_jd, 0, 2, IS_ARRAY, 0)
        ZEND_ARG_TYPE_INFO(0, jd, IS_LONG, 0)
        ZEND_ARG_TYPE_INFO(0, calendar, IS_LONG, 0)
 ZEND_END_ARG_INFO()
 
-ZEND_BEGIN_ARG_INFO_EX(arginfo_cal_info, 0, 0, 0)
+ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_cal_info, 0, 0, IS_ARRAY, 0)
        ZEND_ARG_TYPE_INFO(0, calendar, IS_LONG, 1)
 ZEND_END_ARG_INFO()
 
-ZEND_BEGIN_ARG_INFO_EX(arginfo_cal_to_jd, 0, 0, 4)
+ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_cal_to_jd, 0, 4, IS_LONG, 0)
        ZEND_ARG_TYPE_INFO(0, calendar, IS_LONG, 0)
        ZEND_ARG_TYPE_INFO(0, month, IS_LONG, 0)
        ZEND_ARG_TYPE_INFO(0, day, IS_LONG, 0)
        ZEND_ARG_TYPE_INFO(0, year, IS_LONG, 0)
 ZEND_END_ARG_INFO()
 
-ZEND_BEGIN_ARG_INFO_EX(arginfo_easter_date, 0, 0, 0)
+ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_easter_date, 0, 0, IS_LONG, 0)
        ZEND_ARG_TYPE_INFO(0, year, IS_LONG, 0)
        ZEND_ARG_TYPE_INFO(0, method, IS_LONG, 0)
 ZEND_END_ARG_INFO()
 
-ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_easter_days, 0, 0, IS_LONG, 0)
-       ZEND_ARG_TYPE_INFO(0, year, IS_LONG, 0)
-       ZEND_ARG_TYPE_INFO(0, method, IS_LONG, 0)
-ZEND_END_ARG_INFO()
+#define arginfo_easter_days arginfo_easter_date
 
 ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_frenchtojd, 0, 3, IS_LONG, 0)
        ZEND_ARG_TYPE_INFO(0, month, IS_LONG, 0)
@@ -56,7 +53,7 @@ ZEND_END_ARG_INFO()
 
 #define arginfo_jdtogregorian arginfo_jdtofrench
 
-ZEND_BEGIN_ARG_INFO_EX(arginfo_jdtojewish, 0, 0, 1)
+ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_jdtojewish, 0, 1, IS_STRING, 0)
        ZEND_ARG_TYPE_INFO(0, juliandaycount, IS_LONG, 0)
        ZEND_ARG_TYPE_INFO(0, hebrew, _IS_BOOL, 0)
        ZEND_ARG_TYPE_INFO(0, fl, IS_LONG, 0)
@@ -64,7 +61,7 @@ ZEND_END_ARG_INFO()
 
 #define arginfo_jdtojulian arginfo_jdtofrench
 
-ZEND_BEGIN_ARG_INFO_EX(arginfo_jdtounix, 0, 0, 1)
+ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_jdtounix, 0, 1, IS_LONG, 0)
        ZEND_ARG_TYPE_INFO(0, jday, IS_LONG, 0)
 ZEND_END_ARG_INFO()
 
index 36c851d94f82be0f3b4a7ddcf7912c65c55f4c90..b6e3ee6d6d9946b96f30f10b2158ed4263876a1f 100644 (file)
@@ -49,8 +49,8 @@ static void _cal_easter(INTERNAL_FUNCTION_PARAMETERS, zend_long gm)
        }
 
        if (gm && (year<1970 || year>2037)) {                           /* out of range for timestamps */
-               php_error_docref(NULL, E_WARNING, "This function is only valid for years between 1970 and 2037 inclusive");
-               RETURN_FALSE;
+               zend_value_error("This function is only valid for years between 1970 and 2037 inclusive");
+               return;
        }
 
        golden = (year % 19) + 1;                                       /* the Golden number */
index aeb304e8ec06572047f0f26362277dd1148ebfc8..21dd76d26104dc36e1170de9aa6962f961004bc8 100644 (file)
@@ -6,12 +6,17 @@ edgarsandi - <edgar.r.sandi@gmail.com>
 <?php include 'skipif.inc'; ?>
 --FILE--
 <?php
-var_dump(cal_days_in_month(-1, 4, 2017));
-var_dump(cal_days_in_month(CAL_GREGORIAN,0, 2009));
+try {
+    cal_days_in_month(-1, 4, 2017);
+} catch (ValueError $ex) {
+    echo "{$ex->getMessage()}\n";
+}
+try{
+    cal_days_in_month(CAL_GREGORIAN,0, 2009);
+} catch (ValueError $ex) {
+    echo "{$ex->getMessage()}\n";
+}
 ?>
---EXPECTF--
-Warning: cal_days_in_month(): invalid calendar ID -1 in %s on line %d
-bool(false)
-
-Warning: cal_days_in_month(): invalid date in %s on line %d
-bool(false)
+--EXPECT--
+invalid calendar ID -1
+invalid date
index c4873815acd0a4851f29a815e6396665e7c7bf0c..2a259121c1427ff18465c7335288e28637ff28db 100644 (file)
@@ -6,8 +6,11 @@ edgarsandi - <edgar.r.sandi@gmail.com>
 <?php include 'skipif.inc'; ?>
 --FILE--
 <?php
-var_dump(cal_from_jd(1748326, -1));
+try {
+    cal_from_jd(1748326, -1);
+} catch (ValueError $ex) {
+    echo "{$ex->getMessage()}\n";
+}
 ?>
---EXPECTF--
-Warning: cal_from_jd(): invalid calendar ID -1 in %s on line %d
-bool(false)
+--EXPECT--
+invalid calendar ID -1
index 68f31c16dad1f7595b7719b762b671112bcbaa32..f1d69203bf6d9af8fbcca807cb9e747dc40c6dc0 100644 (file)
@@ -8,9 +8,13 @@ date.timezone=UTC
 <?php
   print_r(cal_info());
   print_r(cal_info(1));
-  print_r(cal_info(99999));
+  try {
+      cal_info(99999);
+  } catch (ValueError $ex) {
+      echo "{$ex->getMessage()}\n";
+  }
 ?>
---EXPECTF--
+--EXPECT--
 Array
 (
     [0] => Array
@@ -212,5 +216,4 @@ Array
     [calname] => Julian
     [calsymbol] => CAL_JULIAN
 )
-
-Warning: cal_info(): invalid calendar ID 99999 in %s on line %d
+invalid calendar ID 99999
index deb5e80e1d25fa0680e00bc5074c2d4a84f5879e..156afaab6fc35c983c58f5ca52550ef2b1b796c4 100644 (file)
@@ -6,8 +6,11 @@ edgarsandi - <edgar.r.sandi@gmail.com>
 <?php include 'skipif.inc'; ?>
 --FILE--
 <?php
-var_dump(cal_to_jd(-1, 8, 26, 74));
+try {
+    cal_to_jd(-1, 8, 26, 74);
+} catch (ValueError $ex) {
+    echo "{$ex->getMessage()}\n";
+}
 ?>
---EXPECTF--
-Warning: cal_to_jd(): invalid calendar ID -1 in %s on line %d
-bool(false)
+--EXPECT--
+invalid calendar ID -1
index 065e1478d0b7bac078953f679384408de0415699..c620e7b441e920c56328bf23b60c799c8c3dc6b3 100644 (file)
@@ -10,12 +10,14 @@ putenv('TZ=UTC');
 echo date("Y-m-d", easter_date(2000))."\n";
 echo date("Y-m-d", easter_date(2001))."\n";
 echo date("Y-m-d", easter_date(2002))."\n";
-echo date("Y-m-d", easter_date(1492))."\n";
+try {
+    easter_date(1492);
+} catch (ValueError $ex) {
+    echo "{$ex->getMessage()}\n";
+}
 ?>
---EXPECTF--
+--EXPECT--
 2000-04-23
 2001-04-15
 2002-03-31
-
-Warning: easter_date(): This function is only valid for years between 1970 and 2037 inclusive in %s on line %d
-1970-01-01
+This function is only valid for years between 1970 and 2037 inclusive
index d6971c0663e35846b3f630fb20bda09fa40b38d2..84f95654300c8c5a49d49278838deefaf450cbe0 100644 (file)
@@ -20,7 +20,11 @@ var_dump(jdtojewish(gregoriantojd(10,28,2002))."\r\n".
 echo jdtojewish(gregoriantojd(11,5,2002)) . "\n";
 echo jdtojewish(gregoriantojd(11,29,2004)) . "\n";
 echo jdtojewish(gregoriantojd(1,1,9998)) . "\n";
-echo jdtojewish(gregoriantojd(1,1,9998),true) . "\n";
+try {
+       jdtojewish(gregoriantojd(1,1,9998),true);
+} catch (ValueError $ex) {
+       echo "{$ex->getMessage()}\n";
+}
 ?>
 --EXPECTF--
 string(%d) "2/22/5763
@@ -38,5 +42,4 @@ string(%d) "2/22/5763
 2/30/5763
 3/16/5765
 3/8/13758
-
-Warning: jdtojewish(): Year out of range (0-9999) in %s on line %d
+Year out of range (0-9999)
index 5d4ea38834d0f419a1c573c7410c76f0ec66d4a3..e47cced1b1accd0fa9869df63802c22d2e02cec7 100644 (file)
@@ -8,7 +8,11 @@ date.timezone=UTC
 <?php include 'skipif.inc'; ?>
 --FILE--
 <?php
-var_dump(jdtounix(2440579)) . PHP_EOL;
+try {
+    jdtounix(2440579);
+} catch (ValueError $ex) {
+    echo $ex->getMessage(), PHP_EOL;
+}
 ?>
 --EXPECT--
-bool(false)
+jday must be within the Unix epoch
index 88de9830acc2019e2c91f315338066025e0ac451..61068ad91eda349fe68223338809709d97bc9a55 100644 (file)
@@ -10,13 +10,17 @@ date.timezone=UTC
 <?php
 putenv('TZ=UTC');
 
-var_dump(unixtojd(-1)) . PHP_EOL;
+try {
+    unixtojd(-1);
+} catch (ValueError $ex) {
+    echo $ex->getMessage(), PHP_EOL;
+}
 var_dump(unixtojd(false)) . PHP_EOL;
 var_dump(unixtojd(null)) . PHP_EOL;
 var_dump(unixtojd(time())) . PHP_EOL;
 ?>
 --EXPECTF--
-bool(false)
+timestamp must not be negative
 int(%d)
 int(%d)
 int(%d)