Skip to content

Conversation

@acasademont
Copy link
Contributor

Hi!

This is my first contribution, all tests keep passing, but I'm not sure the fix is 100% correct. Thanks!!

Fixes https://bugs.php.net/bug.php?id=78326

@WyriHaximus
Copy link
Contributor

WyriHaximus commented Jul 23, 2019

@carusogabriel How hard would a unit test for this be? Nvm I overlooked it 🤐

*ptr = '\0';
ZSTR_LEN(result) = len;
result = zend_string_truncate(result, len, persistent);
ZSTR_VAL(result)[len] = '\0';
Copy link
Member

Choose a reason for hiding this comment

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

You might want to use the same logic as in https://github.com/php/php-src/blob/master/main/streams/streams.c#L759 and only truncate if the "wastage" is large enough.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@nikic thanks! I guess that the potential savings of the truncate are not worth the extra performance costs of the function and memory copy calls if the "waste" is not large enough?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done!

Copy link
Member

Choose a reason for hiding this comment

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

This dropped the ZSTR_LEN(result) = len assignment, which is still needed in case we don't truncate. I'm surprised this doesn't cause test failures.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@nikic you're right, it didn't I'm afraid, any ideas for a test that maybe we could add?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed in any case

Copy link
Member

Choose a reason for hiding this comment

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

<?php
$f = fopen('php://memory', 'rw');
fwrite($f, str_repeat('X', 1000));
fseek($f, 0); 
var_dump(strlen(stream_get_contents($f, 1024)));

should show the issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@nikic it did, thanks! I added the additional test just to be on the safe side

@acasademont acasademont force-pushed the bug78326 branch 2 times, most recently from cc82d91 to 42ddb03 Compare July 24, 2019 09:38
@acasademont
Copy link
Contributor Author

Should I also include a NEWS.txt entry?

@cmb69
Copy link
Member

cmb69 commented Jul 24, 2019

Should I also include a NEWS.txt entry?

No. This is supposed to be done by the "merger".

@nikic
Copy link
Member

nikic commented Jul 29, 2019

Merged as dc7aa22 into 7.2+. Thanks!

@nikic nikic closed this Jul 29, 2019
@acasademont acasademont deleted the bug78326 branch July 30, 2019 08:52
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.

6 participants