Skip to content

Commit

Permalink
Fix CALL_ORIGINAL_FUNCTION_WITH_PARAMS (#14)
Browse files Browse the repository at this point in the history
* fix: simplify hook_strtotime

* change test name

* ptrptr -> ptr

* restore ci and test

* remove debug comment

---------

Co-authored-by: t-matsuno <[email protected]>
  • Loading branch information
zeriyoshi and t-matsuno-777 authored Sep 19, 2024
1 parent 0b9098a commit df5f566
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 49 deletions.
26 changes: 7 additions & 19 deletions ext/hook.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,12 @@ static inline void apply_interval(timelib_time **time, timelib_rel_time *interva
fci->params = _params; \
fci->named_params = NULL; \
fcc->function_handler = zend_hash_str_find_ptr(CG(function_table), #_name, strlen(#_name)); \
zif_handler hook_handler = fcc->function_handler->internal_function.handler; \
fcc->function_handler->internal_function.handler = COLOPL_TS_G(orig_##_name); \
fcc->called_scope = NULL; \
fcc->object = NULL; \
zend_call_function(fci, fcc); \
fcc->function_handler->internal_function.handler = hook_handler; \
efree(fci); \
efree(fcc); \
} while (0);
Expand Down Expand Up @@ -699,25 +701,11 @@ static void hook_strtotime(INTERNAL_FUNCTION_PARAMETERS)
return;
}

if (is_fixed_ret == 2) {
CALL_ORIGINAL_FUNCTION(strtotime);
} else {
/* Call original function with params. */
zval *params = NULL;
uint32_t param_count = 0;
zend_parse_parameters(ZEND_NUM_ARGS(), "+", &params, &param_count);
ZVAL_LONG(&params[1], get_shifted_time(NULL));
CALL_ORIGINAL_FUNCTION_WITH_PARAMS(strtotime, params, param_count);
}

/* Apply interval. */
timelib_time *t = timelib_time_ctor();
timelib_rel_time interval;
timelib_unixtime2gmt(t, Z_LVAL_P(return_value));
get_shift_interval(&interval);
apply_interval(&t, &interval);
RETVAL_LONG(timelib_date_to_int(t, NULL));
timelib_time_dtor(t);
/* Call original function based on shifted time */
zval params[2];
ZVAL_STR(&params[0], times);
ZVAL_LONG(&params[1], get_shifted_time(NULL));
CALL_ORIGINAL_FUNCTION_WITH_PARAMS(strtotime, params, 2);
}

#if HAVE_GETTIMEOFDAY
Expand Down
30 changes: 0 additions & 30 deletions ext/tests/functions/strtotime_extra.phpt

This file was deleted.

33 changes: 33 additions & 0 deletions ext/tests/gh13.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
--TEST--
Check GitHub Issue - #13 (strtotime extra pattern)
--EXTENSIONS--
colopl_timeshifter
--FILE--
<?php

$before = new \DateTime('@' . \strtotime('now'));

\Colopl\ColoplTimeShifter\register_hook(new \DateInterval('P2Y'));

$after_one = new \DateTime('@' . \strtotime('now'));
$after_two = new \DateTime('@' . \strtotime('now'));

$interval = $after_one->diff($before);

if ($before->getTimestamp() == $after_one->getTimestamp()) {
die('failed, $before == $after_one, ' . $before->format('Y-m-d H:i:s.u') . ' / ' . $after_one->format('Y-m-d H:i:s.u') . ' / ' . $after_two->format('Y-m-d H:i:s.u'));
}
if ($before->getTimestamp() == $after_two->getTimestamp()) {
die('failed, $before == $after_two, ' . $before->format('Y-m-d H:i:s.u') . ' / ' . $after_one->format('Y-m-d H:i:s.u') . ' / ' . $after_two->format('Y-m-d H:i:s.u'));
}

/* Note: Sometime valgrind makes flaky: $interval->y !== 2 */
if (($interval->y > 2 && $interval->y !== 0) || $interval->invert !== 0) {
die('failed, $interval->y = ' . $interval->y . ', $interval->invert = ' . $interval->invert);
}

die('success');

?>
--EXPECT--
success

0 comments on commit df5f566

Please sign in to comment.