Skip to content

Trashbin truncate filename unicode - #62698

Open
icewind1991 wants to merge 2 commits into
masterfrom
trashbin-truncate-filename-unicode
Open

Trashbin truncate filename unicode#62698
icewind1991 wants to merge 2 commits into
masterfrom
trashbin-truncate-filename-unicode

Conversation

@icewind1991

@icewind1991 icewind1991 commented Jul 30, 2026

Copy link
Copy Markdown
Member
  • Resolves: #

Summary

The old logic can cut right in the middle of multi-byte unicode codepoints.

TODO

  • ...

Checklist

AI (if applicable)

  • The content of this PR was partly or fully generated using AI

Signed-off-by: Robin Appelman <robin@icewind.nl>
Signed-off-by: Robin Appelman <robin@icewind.nl>
@icewind1991 icewind1991 added this to the Nextcloud 35 milestone Jul 30, 2026
@icewind1991
icewind1991 requested a review from a team as a code owner July 30, 2026 14:01
@icewind1991 icewind1991 added the 3. to review Waiting for reviews label Jul 30, 2026
@icewind1991
icewind1991 requested review from Altahrim, come-nc, leftybournes and salmart-dev and removed request for a team July 30, 2026 14:01
@icewind1991

Copy link
Copy Markdown
Member Author

/backport to stable34

@icewind1991

Copy link
Copy Markdown
Member Author

/backport to stable33

@icewind1991

Copy link
Copy Markdown
Member Author

/backport to stable32

ShGKme
ShGKme previously approved these changes Jul 30, 2026

$charsToRemove = $length - $maxLength + 1;
$charLength = mb_strlen($trashFilename);
$start = mb_substr($trashFilename, 0, floor(($charLength / 2) - $charsToRemove));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
$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));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
$end = mb_substr($trashFilename, floor($charLength / 2));
$end = mb_substr($trashFilename, intdiv($charLength, 2));

Comment on lines 1210 to +1220
$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;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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
ShGKme dismissed their stale review July 30, 2026 21:52

Outdated

@ShGKme ShGKme left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

To remove the middle instead of "after the middle"

Comment on lines +1215 to +1220

$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;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
$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);

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.

3 participants