Skip to content

Refactor proc_open() implementation #7255

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

Merged
merged 7 commits into from
Aug 11, 2021
Merged

Conversation

Girgias
Copy link
Member

@Girgias Girgias commented Jul 17, 2021

Not sure all of the commits are sensible, and I still don't quite grasp why I segfault on an unknown address if I convert the command struct member from char* to zend_string*

@Girgias Girgias force-pushed the proc_open-refacotr branch from 0c7d6f6 to 0afe7da Compare July 17, 2021 16:09
@twose
Copy link
Member

twose commented Jul 19, 2021

I think you should use add_assoc_str(return_value, "command", zend_string_copy(proc->command)); instead, this may be the cause of the memory error :)

@Girgias Girgias force-pushed the proc_open-refacotr branch from 60fd91e to 0ba3a6f Compare July 19, 2021 09:15
@Girgias
Copy link
Member Author

Girgias commented Jul 19, 2021

I think you should use add_assoc_str(return_value, "command", zend_string_copy(proc->command)); instead, this may be the cause of the memory error :)

That seems to have been indeed the issue, thanks for spotting this. :-)

@nikic
Copy link
Member

nikic commented Jul 19, 2021

Looks mostly okay. I'm uncertain about:

  • a112c2d The types used here look rather arbitrary to me, why is this an improvement?
  • 1d05bba IMHO this should've just changed the return types to zend_result. Or do we have some general policy that bool is preferred over zend_result?

@Girgias
Copy link
Member Author

Girgias commented Jul 19, 2021

Looks mostly okay. I'm uncertain about:

* [a112c2d](https://github.com/php/php-src/commit/a112c2d35624f414558f6a1b01b24e522d34d417) The types used here look rather arbitrary to me, why is this an improvement?

That's the one I really wasn't sure about, just tried to align some types to prevent the int cast, and thought well the array offsets used here should only be unsigned so let's go with size_t, but it doesn't really do anything.

* [1d05bba](https://github.com/php/php-src/commit/1d05bba8e88b24bb70c069679f48e55bc889a7b4) IMHO this should've just changed the return types to zend_result. Or do we have some general policy that bool is preferred over zend_result?

We don't have a general policy but I do think bool is usually better than zend_result because you cannot do if (!func_return_zend_result()) {} to check for failure, one option would be to change zend_result to match bool, but the -1 for FAILURE is probably used (if not within php-src, in external extensions) to return -1 for failure as is typical for C APIs.
And because those are static functions restrained to this file, I prefer using bool to make a future audit of FAILURE/SUCCESS easier.

But I could change it to zend_result.

@nikic
Copy link
Member

nikic commented Jul 19, 2021

We don't have a general policy but I do think bool is usually better than zend_result because you cannot do if (!func_return_zend_result()) {} to check for failure, one option would be to change zend_result to match bool, but the -1 for FAILURE is probably used (if not within php-src, in external extensions) to return -1 for failure as is typical for C APIs.
And because those are static functions restrained to this file, I prefer using bool to make a future audit of FAILURE/SUCCESS easier.

Why is being able to write if (!func()) a goal though? The == SUCCESS / == FAILURE check seems pretty clear for a function where the return value indicates success/failure. bool is good for predicate functions (is_* and friends), but it doesn't seems like the natural choice for a success indicator, if we already have zend_result that is explicitly intended for that purpose?

@Girgias
Copy link
Member Author

Girgias commented Jul 19, 2021

It is not a goal, maybe it's just because zend_result is in my mind also a "boolean"-like enum that I think it's better to just use the "native" type directly, especially as one needs to "audit" the functions anyway to check the int return value is legitimate or one can change it to zend_result. Also it can be a bit confusing at times that some return zend_result and other bool.

But, frankly either zend_result and bool are fine with me.

@Girgias Girgias force-pushed the proc_open-refacotr branch from 0d7f203 to 81cbbcf Compare August 11, 2021 10:56
@Girgias Girgias requested a review from nikic August 11, 2021 11:34
@Girgias
Copy link
Member Author

Girgias commented Aug 11, 2021

I dropped the int to size_t type changes and changed the bool returns to zend_result

@Girgias Girgias merged commit 98eb60a into php:master Aug 11, 2021
@Girgias Girgias deleted the proc_open-refacotr branch August 11, 2021 12:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants