Skip to content

fix(webserver): php_core_lib various fixes - #150

Merged
got3nks merged 5 commits into
amule-org:masterfrom
RealGreenDragon:fix_php_core_lib
Jun 14, 2026
Merged

fix(webserver): php_core_lib various fixes#150
got3nks merged 5 commits into
amule-org:masterfrom
RealGreenDragon:fix_php_core_lib

Conversation

@RealGreenDragon

Copy link
Copy Markdown

Fixes:

  • Removed php_native_substr as it contains several bugs and above all is never used
  • Moved NULL check before params dereference in functions php_native_substr, php_native_gettext, php_native_gettext_noop, and php_native_ngettext
  • Replaced PRIu64 with PRId64 in php_var_dump (PHP_VAL_INT can hold a signed integer)
  • Replaced while ( strlen(scan_ptr) ) with while ( *scan_ptr ) in CPhpFilter::CPhpFilter, making the check O(1) instead of O(N)

Doubts and suggestions:

  • php_native_isset calls cast_value_str on params without checking param->str_val (so it seems to do nothing useful); the only apparent reason is to trigger assert(0) when type=PHP_VAL_INT_DATA, but I don't see the advantage → two options: remove params and the cast_value_str call entirely, or also check whether param->str_val is not an empty string (but this seems unnecessary as the type check alone should be sufficient)
  • php_native_htmlspecialchars calls cast_value_dnum on result without an apparent reason (result->int_val is never used); the only apparent reason seems to be triggering assert(0) when type=PHP_VAL_INT_DATA, but I don't see the advantage → remove the cast_value_dnum call
  • CWriteStrBuffer::Write: in if ( (len + 1) <= m_curr_buf_left ), len is incremented by 1 to account for the string terminator, but strncpy does not actually add it → two options: explicitly add the null terminator (it is unclear whether the current behavior is intentional), or replace (len + 1) with len in the condition (also replacing strncpy with memcpy to make the intent clearer)

If you can help clarify these doubts, I will apply the suggested changes in this PR.

@RealGreenDragon RealGreenDragon changed the title Fix php core lib fix(webserver): php_core_lib various fixes Jun 14, 2026

@got3nks got3nks left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Thanks. Verified the four fixes:

  • PRId64 matches PHP's signed-integer semantics — fine. Worth noting though: int_val is declared as uint64_t in php_syntree.h:67, and there's a second PRIu64 print of the same field at php_syntree.cpp:965 (the cast_value_str path) that this PR doesn't touch. After this lands, the codebase will display the field as signed in one place and unsigned in the other. Easiest tidy-up: flip that line to PRId64 too (same justification as php_var_dump), either in this PR or a small follow-up. The "right" long-term fix is to change int_val to int64_t in the struct so the storage type matches the semantics, but that's a wider change.
  • php_native_substr removal — confirmed unused (grep -rn "php_native_substr\|\"substr\"" src/webserver returns only the definition itself).
  • NULL-before-deref fixes in the four functions are textbook UB fixes.
  • while ( *scan_ptr ) over strlen(scan_ptr) — net behaviour identical, O(N) → O(1) per iteration.

On the three doubts:

  1. php_native_isset cast_value_str calls without checking str_val — leave it. The cast_value_str calls normalise the value type; the function's intent is to mimic PHP's isset() which checks whether the variable is set + non-null. The current behaviour matches enough of that semantics; removing the calls risks subtle behaviour changes on edge cases (PHP_VAL_NONE, PHP_VAL_INT_DATA). I'd skip this one in this PR.

  2. php_native_htmlspecialchars cast_value_dnum on result — dead but harmless. The function later overwrites result with a string. Removing the cast_value_dnum is safe; keeping it is wasted cycles. Author's call — fine either way. Not in this PR's scope.

  3. CWriteStrBuffer::Write off-by-one — real bug. strncpy(dst, src, n) does NOT null-terminate when strlen(src) >= n. The (len + 1) <= m_curr_buf_left check reserves a byte for the terminator, but nothing actually writes it. Two fixes either is acceptable:

    • Append dst[len] = '\0'; after strncpy.
    • Switch to memcpy(dst, src, len); dst[len] = '\0'; (clearer intent, no strncpy padding behaviour).

    Worth doing as a small follow-up rather than dragging it into this PR — different file, different concern.

Approving as-is — the line 965 PRIu64 is the only thing worth picking up in this PR if you want symmetry, otherwise a follow-up is fine.

@got3nks
got3nks merged commit 613fe66 into amule-org:master Jun 14, 2026
9 checks passed
@RealGreenDragon
RealGreenDragon deleted the fix_php_core_lib branch June 14, 2026 12:24
got3nks added a commit that referenced this pull request Jun 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants