Skip to content

CUtlString::TrimRight sets wrong length #1182

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

Open
Adrianilloo opened this issue Apr 4, 2025 · 1 comment
Open

CUtlString::TrimRight sets wrong length #1182

Adrianilloo opened this issue Apr 4, 2025 · 1 comment

Comments

@Adrianilloo
Copy link

Adrianilloo commented Apr 4, 2025

Both overloads set an excess length by 1 more character when any trimming succeeds:

SetLength( nIndex + 2 );

SetLength( i + 2 );

Should be changed to SetLength( <index var> + 1 );

@Tripperful
Copy link

Tripperful commented Apr 5, 2025

It is correct.

Consider these 2 lines you linked:

// "baz\0"
//  012 3
m_pString[nIndex + 1] = 0; // index of \0 in "baz\0" = 3, which means nIndex here = 2
SetLength( nIndex + 2 ); // since nIndex here = 2, nIndex + 2 = 4, and "baz\0" is indeed 4 characters long

CUtlString::SetLength( int nLen ) accepts literal buffer length, including the string null-terminator, you can verify this by checking its code where it calls AllocMemory( nLen );, implying that this buffer size should be enough for the '\0' as well.

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

No branches or pull requests

2 participants