Skip to content

Fix GH-19371: integer overflow in calendar.c #19380

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions ext/calendar/calendar.c
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,16 @@ PHP_FUNCTION(cal_days_in_month)
RETURN_THROWS();
}

if (UNEXPECTED(month <= 0 || month > INT32_MAX - 1)) {
zend_argument_value_error(2, "must be between 1 and %d", INT32_MAX - 1);
RETURN_THROWS();
}

if (UNEXPECTED(year > INT32_MAX - 1)) {
zend_argument_value_error(3, "must be less than %d", INT32_MAX - 1);
RETURN_THROWS();
}

calendar = &cal_conversion_table[cal];

sdn_start = calendar->to_jd(year, month, 1);
Expand Down Expand Up @@ -239,6 +249,21 @@ PHP_FUNCTION(cal_to_jd)
RETURN_THROWS();
}

if (UNEXPECTED(month <= 0 || month > INT32_MAX - 1)) {
zend_argument_value_error(2, "must be between 1 and %d", INT32_MAX - 1);
RETURN_THROWS();
}

if (UNEXPECTED(ZEND_LONG_EXCEEDS_INT(day))) {
Copy link
Member

@devnexen devnexen Aug 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just curious. if you want to insist on INT32 ranges why not doing the range check "manually" here ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code does not do +1/-1 on the day, so that's fine.
ZEND_LONG_EXCEEDS_INT will also avoid a warning on 32-bit systems where zend_long==int.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah yes I missed the former.. ok !

zend_argument_value_error(3, "must be between %d and %d", INT32_MIN, INT32_MAX);
RETURN_THROWS();
}

if (UNEXPECTED(year > INT32_MAX - 1)) {
zend_argument_value_error(4, "must be less than %d", INT32_MAX - 1);
RETURN_THROWS();
}

RETURN_LONG(cal_conversion_table[cal].to_jd(year, month, day));
}
/* }}} */
Expand Down
2 changes: 1 addition & 1 deletion ext/calendar/tests/cal_days_in_month_error1.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ try {
echo "{$ex->getMessage()}\n";
}
try{
cal_days_in_month(CAL_GREGORIAN,0, 2009);
cal_days_in_month(CAL_GREGORIAN,20, 2009);
} catch (ValueError $ex) {
echo "{$ex->getMessage()}\n";
}
Expand Down
61 changes: 61 additions & 0 deletions ext/calendar/tests/gh19371.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
--TEST--
GH-19371 (integer overflow in calendar.c)
--SKIPIF--
<?php if (PHP_INT_SIZE !== 8) die("skip only for 64-bit"); ?>
--EXTENSIONS--
calendar
--FILE--
<?php

try {
echo cal_days_in_month(CAL_GREGORIAN, 12, PHP_INT_MAX);
} catch (ValueError $e) {
echo $e->getMessage(), "\n";
}
try {
echo cal_days_in_month(CAL_GREGORIAN, PHP_INT_MIN, 1);
} catch (ValueError $e) {
echo $e->getMessage(), "\n";
}
try {
echo cal_days_in_month(CAL_GREGORIAN, PHP_INT_MAX, 1);
} catch (ValueError $e) {
echo $e->getMessage(), "\n";
}

try {
echo cal_to_jd(CAL_GREGORIAN, PHP_INT_MIN, 1, 1);
} catch (ValueError $e) {
echo $e->getMessage(), "\n";
}
try {
echo cal_to_jd(CAL_GREGORIAN, PHP_INT_MAX, 1, 1);
} catch (ValueError $e) {
echo $e->getMessage(), "\n";
}
try {
echo cal_to_jd(CAL_GREGORIAN, 1, PHP_INT_MIN, 1);
} catch (ValueError $e) {
echo $e->getMessage(), "\n";
}
try {
echo cal_to_jd(CAL_GREGORIAN, 1, PHP_INT_MAX, 1);
} catch (ValueError $e) {
echo $e->getMessage(), "\n";
}
try {
echo cal_to_jd(CAL_GREGORIAN, 1, 1, PHP_INT_MAX);
} catch (ValueError $e) {
echo $e->getMessage(), "\n";
}

?>
--EXPECT--
cal_days_in_month(): Argument #3 ($year) must be less than 2147483646
cal_days_in_month(): Argument #2 ($month) must be between 1 and 2147483646
cal_days_in_month(): Argument #2 ($month) must be between 1 and 2147483646
cal_to_jd(): Argument #2 ($month) must be between 1 and 2147483646
cal_to_jd(): Argument #2 ($month) must be between 1 and 2147483646
cal_to_jd(): Argument #3 ($day) must be between -2147483648 and 2147483647
cal_to_jd(): Argument #3 ($day) must be between -2147483648 and 2147483647
cal_to_jd(): Argument #4 ($year) must be less than 2147483646
Loading