Trashbin truncate filename unicode - #62698
Conversation
Signed-off-by: Robin Appelman <robin@icewind.nl>
Signed-off-by: Robin Appelman <robin@icewind.nl>
|
/backport to stable34 |
|
/backport to stable33 |
|
/backport to stable32 |
|
|
||
| $charsToRemove = $length - $maxLength + 1; | ||
| $charLength = mb_strlen($trashFilename); | ||
| $start = mb_substr($trashFilename, 0, floor(($charLength / 2) - $charsToRemove)); |
There was a problem hiding this comment.
apps/files_trashbin/lib/Trashbin.php:1218:42: InvalidScalarArgument: Argument 3 of mb_substr expects int|null, but float provided (see https://psalm.dev/012)
We don't have strict_types?
There was a problem hiding this comment.
| $start = mb_substr($trashFilename, 0, floor(($charLength / 2) - $charsToRemove)); | |
| $start = mb_substr($trashFilename, 0, intdiv($charLength, 2) - $charsToRemove)); |
| $charsToRemove = $length - $maxLength + 1; | ||
| $charLength = mb_strlen($trashFilename); | ||
| $start = mb_substr($trashFilename, 0, floor(($charLength / 2) - $charsToRemove)); | ||
| $end = mb_substr($trashFilename, floor($charLength / 2)); |
There was a problem hiding this comment.
| $end = mb_substr($trashFilename, floor($charLength / 2)); | |
| $end = mb_substr($trashFilename, intdiv($charLength, 2)); |
| $length = strlen($trashFilename); | ||
| // oc_filecache `name` column has a limit of 250 chars | ||
| $maxLength = 250; | ||
| if ($length > $maxLength) { | ||
| $trashFilename = substr_replace( | ||
| $trashFilename, | ||
| '', | ||
| $maxLength / 2, | ||
| $length - $maxLength | ||
| ); | ||
| // truncate at the middle, since the last characters are fairly likely to have meaningful information such as version numbering | ||
|
|
||
| $charsToRemove = $length - $maxLength + 1; | ||
| $charLength = mb_strlen($trashFilename); | ||
| $start = mb_substr($trashFilename, 0, floor(($charLength / 2) - $charsToRemove)); | ||
| $end = mb_substr($trashFilename, floor($charLength / 2)); | ||
| return $start . '_' . $end; |
There was a problem hiding this comment.
Is it possible that the length is, e.g., 1000.
charsToRemove = 750
floor(($charLength / 2) - $charsToRemove = 1000 / 2 - 750 = -250 - negative value.
With floor(($charLength / 2) - $charsToRemove) we remove not IN the middle, but "after" the middle.
ShGKme
left a comment
There was a problem hiding this comment.
To remove the middle instead of "after the middle"
|
|
||
| $charsToRemove = $length - $maxLength + 1; | ||
| $charLength = mb_strlen($trashFilename); | ||
| $start = mb_substr($trashFilename, 0, floor(($charLength / 2) - $charsToRemove)); | ||
| $end = mb_substr($trashFilename, floor($charLength / 2)); | ||
| return $start . '_' . $end; |
There was a problem hiding this comment.
| $charsToRemove = $length - $maxLength + 1; | |
| $charLength = mb_strlen($trashFilename); | |
| $start = mb_substr($trashFilename, 0, floor(($charLength / 2) - $charsToRemove)); | |
| $end = mb_substr($trashFilename, floor($charLength / 2)); | |
| return $start . '_' . $end; | |
| $half = intdiv($maxLength - 1, 2); // One char for _ | |
| return mb_substr($trashFilename, 0, $half) | |
| . '_' | |
| . mb_substr($trashFilename, -$half); |
Summary
The old logic can cut right in the middle of multi-byte unicode codepoints.
TODO
Checklist
3. to review, feature component)stable32)AI (if applicable)