Fix OpenArena on glibc 2.37+ and replace all strncpy calls#659
Closed
zturtleman wants to merge 4 commits intoioquake:mainfrom
Closed
Fix OpenArena on glibc 2.37+ and replace all strncpy calls#659zturtleman wants to merge 4 commits intoioquake:mainfrom
zturtleman wants to merge 4 commits intoioquake:mainfrom
Conversation
It can't overflow because buff and text have the same max length (1024 and MAX_TOKENLENGTH).
macOS (targeting newer SDKs) and glibc 2.37 changes broke overlapping strncpy() used by OpenArena 0.8.8 QVMs. (Technically it was undefined behavior.) Add Q_strncpy() by Eugene C. from Quake3e that allows overlapping dest and src for QVM strncpy syscalls. I made Q_strncpyz() use it for consistency and made QVMs remap the Q_strncpy() call to the strncpy syscall in case there is a better idea of how to handle strncpy in the future. Try to handle strncpy the same in QVMs and DLLs.
Author
|
mgerhardy
added a commit
to PadWorld-Entertainment/worldofpadman
that referenced
this pull request
May 30, 2024
ioquake/ioq3#659 remapping strncpy to the new Q_strncpy is still missing
|
I'm implemented both char *Q_strncpy( char *dest, char *src, int destsize )
{
char *s = src, *start = dest;
int src_len;
while ( *s != '\0' )
++s;
src_len = (int)(s - src);
if ( src_len > destsize ) {
src_len = destsize;
}
destsize -= src_len;
if ( dest > src && dest < src + src_len ) {
int i;
#ifdef _DEBUG
Com_Printf( S_COLOR_YELLOW "Q_strncpy: overlapped (dest > src) buffers\n" );
#endif
for ( i = src_len - 1; i >= 0; --i ) {
dest[i] = src[i]; // back overlapping
}
dest += src_len;
} else {
#ifdef _DEBUG
if ( src >= dest && src < dest + src_len ) {
Com_Printf( S_COLOR_YELLOW "Q_strncpy: overlapped (src >= dst) buffers\n" );
#ifdef _MSC_VER
// __debugbreak();
#endif
}
#endif
while ( src_len > 0 ) {
*dest++ = *src++;
--src_len;
}
}
while ( destsize > 0 ) {
*dest++ = '\0';
--destsize;
}
return start;
}it is longer than any suggested/existing version but fully inline and do not have any references to external functions like If you want to use it in QVM code then you should replace all post-increment assignments like |
Author
|
Thank you. I don't compile it in QVMs, it's only called it through the strncpy syscall. |
This was referenced Jul 24, 2025
Author
mgerhardy
added a commit
to PadWorld-Entertainment/worldofpadman
that referenced
this pull request
Jul 26, 2025
mgerhardy
added a commit
to PadWorld-Entertainment/worldofpadman
that referenced
this pull request
Aug 3, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
macOS (targeting newer SDKs) and glibc 2.37 changes broke overlapping dest and src in strncpy() used by OpenArena 0.8.8 QVMs. (Technically it was undefined behavior.)
Add Q_strncpy() by Eugene C. from Quake3e that allows overlapping dest and src for QVM strncpy syscalls.
I made everything use Q_strncpyz and it use the new Q_strncpy function. I made QVMs remap the Q_strncpy call to the strncpy syscall in case there is a better idea of how to handle it in the future. I tried to make "strncpy" behave the same in QVMs and DLLs.
Also fix a CGame network buffer overflow due to mishandling of strncpy. NUL terminator is great but it's better when it's in the destination buffer.
Fixes #639.