Skip to content

fix(webserver): php_native_split function various issues - #146

Merged
got3nks merged 3 commits into
amule-org:masterfrom
RealGreenDragon:fix_php_native_split
Jun 13, 2026
Merged

fix(webserver): php_native_split function various issues#146
got3nks merged 3 commits into
amule-org:masterfrom
RealGreenDragon:fix_php_native_split

Conversation

@RealGreenDragon

Copy link
Copy Markdown

Summary

Fix various issues in the php_native_split function:

  • Fixed an error raised when the optional split_limit parameter was missing (the error message also incorrectly reported 'string' instead of 'limit' as the parameter name) removing the useless else branch since the parameter is optional.
  • Enforced split_limit in the function by introducing a piece_count variable to track segments and added a check at the start of the while loops (in both branches).
  • Prevented an infinite loop on zero-length matches by adding a guard condition (in both branches).
  • Standardized 'if' statement spacing and comments for consistency (in both branches).

Test plan

N/A

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

Two small things worth fixing before merge:

  1. Sign-compare warningpiece_count is int, split_limit is unsigned int, so piece_count >= split_limit - 1 will warn under -Wsign-compare in both branches. Making piece_count unsigned int cleans it up.

  2. Limit convention — PHP's split($pattern, $string, $limit = -1) uses -1 as "no limit". The PR treats 0 as the default and only positive values as the cap, so a caller passing PHP's documented -1 would underflow to UINT_MAX (accidentally unlimited). Switching split_limit to signed int with -1 default and checking split_limit > 0 aligns with PHP semantics and avoids the underflow.

The four core fixes themselves all check out. Thanks for the contribution.

@RealGreenDragon

RealGreenDragon commented Jun 13, 2026

Copy link
Copy Markdown
Author

I made split_limit an int to avoid the warning and switched split_limit default at -1 with error if split_limit<=0.

Please say me if now it is ok and if I need to squash.

@RealGreenDragon
RealGreenDragon requested a review from got3nks June 13, 2026 16:10
@RealGreenDragon

RealGreenDragon commented Jun 13, 2026

Copy link
Copy Markdown
Author

I missed one change in PR description: nmatch = string length is useless as only pmatch[0] used, so changed it to 1 (fixed value).

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

Drop the if ( split_limit <= 0 ) block — no return after it makes it inconsistent with the other error paths in this function, and -1 is the PHP-documented sentinel for "no limit" so emitting an error on it defeats the convention. The -1 default + the loop's split_limit > 0 gate already handle "no limit" cleanly.

si = get_scope_item(g_current_scope, "__param_2");
if ( si ) {
    PHP_VALUE_NODE *limit_node = &si->var->value;
    cast_value_dnum(limit_node);
    split_limit = limit_node->int_val;
}

No need to squash.

@RealGreenDragon

Copy link
Copy Markdown
Author

You are right, sorry, fix applied.

@RealGreenDragon
RealGreenDragon requested a review from got3nks June 13, 2026 16:39

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

Now matches PHP semantics cleanly — -1 default, split_limit > 0 gate handles "no limit" silently, no spurious error. Thanks for the back-and-forth.

@got3nks
got3nks merged commit b41c4f6 into amule-org:master Jun 13, 2026
9 checks passed
@RealGreenDragon
RealGreenDragon deleted the fix_php_native_split branch June 14, 2026 07:01
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