Skip to content

Fix OpenArena on glibc 2.37+ and replace all strncpy calls#659

Closed
zturtleman wants to merge 4 commits intoioquake:mainfrom
zturtleman:fix-overlapping-strncpy
Closed

Fix OpenArena on glibc 2.37+ and replace all strncpy calls#659
zturtleman wants to merge 4 commits intoioquake:mainfrom
zturtleman:fix-overlapping-strncpy

Conversation

@zturtleman
Copy link
Copy Markdown

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.

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.
@zturtleman zturtleman marked this pull request as draft May 20, 2024 19:32
@zturtleman
Copy link
Copy Markdown
Author

  • I still need to test glibc 2.37+.
  • I should probably reword "allows overlapping dest and src" as it only works correctly if dest <= src.
  • Still debating just using an ifdef Q3_VM in Q_strncpyz to call strncpy instead of adding Q_strncpy in *_syscalls.asm.

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
@ec-
Copy link
Copy Markdown

ec- commented Jun 7, 2024

I'm implemented both src >= dst and src < dst cases :

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 memmove()

If you want to use it in QVM code then you should replace all post-increment assignments like *dest++ = '\0'; with *dest = '\0'; ++dest; because LCC generates suboptimal code for such cases

@zturtleman
Copy link
Copy Markdown
Author

Thank you. I don't compile it in QVMs, it's only called it through the strncpy syscall.

@zturtleman
Copy link
Copy Markdown
Author

Closing; I'm replaced this pull request with #741 and #742.

@zturtleman zturtleman closed this Jul 24, 2025
@zturtleman zturtleman deleted the fix-overlapping-strncpy branch July 24, 2025 05:01
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
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

Successfully merging this pull request may close these issues.

2 participants