[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 112: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/functions.php on line 4787: Cannot modify header information - headers already sent by (output started at [ROOT]/includes/functions.php:3922)
[phpBB Debug] PHP Warning: in file [ROOT]/includes/functions.php on line 4789: Cannot modify header information - headers already sent by (output started at [ROOT]/includes/functions.php:3922)
[phpBB Debug] PHP Warning: in file [ROOT]/includes/functions.php on line 4790: Cannot modify header information - headers already sent by (output started at [ROOT]/includes/functions.php:3922)
[phpBB Debug] PHP Warning: in file [ROOT]/includes/functions.php on line 4791: Cannot modify header information - headers already sent by (output started at [ROOT]/includes/functions.php:3922)
InsideQC Forums • View topic - String Safety

String Safety

Discuss programming topics for the various GPL'd game engine sources.

Moderator: InsideQC Admins

Re: String Safety

Postby revelator » Thu Apr 05, 2012 8:39 am

Sorry havent had time to test it yet but i suppose it should work also atleast it should give a hint as to where the buffer overflows.
Productivity is a state of mind.
User avatar
revelator
 
Posts: 2605
Joined: Thu Jan 24, 2008 12:04 pm
Location: inside tha debugger

Re: String Safety

Postby revelator » Thu Apr 05, 2012 11:39 am

this might explain it a bit better than i can.



look at the last comment.

besides the return on buffer overflow it actually works correctly but it makes it a pita to correct your code when the return simply kills your app,
or am i being to harsh here ?.

btw only trying to help here :) i dont intend to derail an otherwise interresting thread.
Productivity is a state of mind.
User avatar
revelator
 
Posts: 2605
Joined: Thu Jan 24, 2008 12:04 pm
Location: inside tha debugger

Re: String Safety

Postby revelator » Fri Apr 06, 2012 12:36 am

im wondering a bit if we could malloc the buffer dynamically ? that way it just grows to what ever size the buffer array needs.
though things like char string[somebuffersize]; would have to go but with a few checks we could probably ensure it newer grows larger than what the OS can handle hmm ?.
or would it chew to many resources ?.

btw the short answer to the question if _vsnprintf returns -1 is yes it triggers the crt buffer overflow check in msvcrt and kills the app but im not 100% it applies to the wrappers most of us use.
including the Q3 ones.
Productivity is a state of mind.
User avatar
revelator
 
Posts: 2605
Joined: Thu Jan 24, 2008 12:04 pm
Location: inside tha debugger

Re: String Safety

Postby Knightmare » Sat Apr 07, 2012 7:02 pm

I induced an _vnsprinf overflow in KMQ2 (built under VS2008, I _vsnprinted the startup GL extension strings into a too-small buffer), and it didn't cause a crash. There were a few corrupt-looking chars at the end of the text, but KMQ2 didn't experience an error or crash.

The only place in KMQ2 where the return value from _vsnprint is checked is Com_sprintf, where it uses a 64KB temp buffer (far larger than the destination buffers it's copied into). So I don't think there's an actual cause for concern here.
Knightmare
 
Posts: 63
Joined: Thu Feb 09, 2012 1:55 am

Re: String Safety

Postby revelator » Sun Apr 08, 2012 12:31 am

its a bit strange... i get the crash when -1 returned on some versions of the msvcrt runtime but not all.
if it helps someone feel free to use the bsd version i posted but else dont since its so big.
Productivity is a state of mind.
User avatar
revelator
 
Posts: 2605
Joined: Thu Jan 24, 2008 12:04 pm
Location: inside tha debugger

Re: String Safety

Postby taniwha » Thu May 10, 2012 3:22 am

I might be coming into this a bit late, but QuakeForge has had "dynamic strings" (dstring) for a long time now (written by snax). Probably not that different (conceptually) from Sajt's stuff. They've almost completely replaced all occurrences of str*, and have even been pressed into service for things like building vertex arrays :shock:. Good old "va" is still heavily used (implemented using dstrings).

dsprintf, dasprintf, dvsprint and davsprintf ("a" appends the print output to the existing string).

String copying, inserting, appending, both null-terminated and "byte buffer", all supported. I doubt I've really used them to their full potential.

re _vnsprintf/vnsprintf: I've never experienced any problems with it in windows. I have, however, had a lot of fun with it in linux on various hardware (ppc etc), but VA_COPY and friends took care of that. However, looking at that msdn page, it looks like I should be passing size -1 rather than size. Easy fix.

That -1: just a while (vsnprintf (...) == -1) {grow buffer} No need for a special case for windows vs everybody else (actually, some other libc versions do -1 as well), though you do have to check whether you still need to grow the buffer if vsnprintf is implemented correctly and re-print, but it's still quite clean code. The only #if code in there is for VA_COPY.
Leave others their otherness.
http://quakeforge.net/
taniwha
 
Posts: 399
Joined: Thu Jan 14, 2010 7:11 am

Re: String Safety

Postby Baker » Thu May 10, 2012 9:51 am

The night is young. How else can I annoy the world before sunsrise? 8) Inquisitive minds want to know ! And if they don't -- well like that ever has stopped me before ..
User avatar
Baker
 
Posts: 3666
Joined: Tue Mar 14, 2006 5:15 am

Re: String Safety

Postby Spike » Thu May 10, 2012 10:23 am

try running FTE on a bigendian machine and you'll see if it supports it or not. :P
If I can't test it then I'm not going to pretend that it works.
Either way, the only mainstream system that really uses bigendian is old ppc macs, and those are slowly dying out.
I am ignoring PS3 consoles with the 'Other OS' option, but Sony also tried killing that too.
Its possible to get bigendian arm chips, but most are in littleendian mode.
Regarding FTE though, Bigfoot occasionally runs FTE on his MorphOS machines, which are bigendian ppc thingies. While its possible that certain extra features are not endian safe, the core featureset is or he would have complained about that by now.
For me, the easiest way to handle endian is to read/write one byte at a time, which is required for misaligned accesses (eg: arm) anyway, thus its silly to be calling LittleLong etc on incoming network data.
Ethernet through to UDP/TCP are all big-endian, yes, thus routers are often bigendian. (Net)Quake's transport layer is also bigendian, but the payload itself is little endian, if I remember correctly. Vanilla QuakeWorld will fail if not also fixed for bigendian (map hashes will kick you), but its protocol is fully little endian.

_vnsprintf on windows does not guarentee null termination. subtracting 1 is about as effective as just overwriting that byte afterwards too, which might give an easier compat path anyway. The return argument is more problematic, assuming you have something like quakeforge's dynamic strings. :P
Not sure msvc6 even has VA_COPY, but being x86 and fully stack based, its just a pointer or something trivial. I seem to remember having issues on more current chips, but I don't remember if it was arm or amd64 or both, either way VA_COPY is the proper way to fix that stuff (Baker: not an endian issue!).
.
Spike
 
Posts: 2914
Joined: Fri Nov 05, 2004 3:12 am
Location: UK

Re: String Safety

Postby Baker » Thu May 10, 2012 11:05 am

I've been working with file formats lately and he said "ppc" so I drifted into off topic nonsequitur. :D
The night is young. How else can I annoy the world before sunsrise? 8) Inquisitive minds want to know ! And if they don't -- well like that ever has stopped me before ..
User avatar
Baker
 
Posts: 3666
Joined: Tue Mar 14, 2006 5:15 am

Re: String Safety

Postby taniwha » Fri May 11, 2012 1:54 am

I know of no x86 ABI that has VA_COPY (no need). ppc was the first one I encountered that needed it, then maybe mips (ps2), alpha and maybe arm. I wouldn't be surprised if pa-risc and i860 (? been too long. intel's crazy mixed-endian risc cpu) needed it too, but I wasn't doing much vsnprintf stuff then.

Every now and then, I get bug reports about endian issues with QF (solaris etc), but yeah, big-endian systems are rare these days. Continuing to support big-endian systems isn't a major problem for qf: the Little* functions just wind up as no-op assignments (a = LittleLong(a); -> a = a;). It's a problem only when I forget and somebody tries QF on something obscure.

Back to string safety: rather than worrying about getting strncpy/strncat (!!!) etc right every time (and worse, having arbitrary limits on strings), I prefer to use a nice dynamic string library: very difficult to mess up.
Leave others their otherness.
http://quakeforge.net/
taniwha
 
Posts: 399
Joined: Thu Jan 14, 2010 7:11 am

Re: String Safety

Postby Spike » Fri May 11, 2012 2:13 am

va_copy with no capitals is part of c99. 12+ years later and msvc still doesn't support va_copy.
ZQuake/FuhQuake/EZQuake/FTE all have a Q_strncpyz, which is basically strlcpy with no return value.
strlcpy/strlcat do just as you expect them to, rather than the annoying strncpy/bizzare strncat functions. Yay for BSD.
Its not infinite length, but it does the job. :)
.
Spike
 
Posts: 2914
Joined: Fri Nov 05, 2004 3:12 am
Location: UK

Re: String Safety

Postby revelator » Fri May 11, 2012 2:37 am

BSD has rolled out some nice stuff indeed :) I use several of there functions on windows as replacements for not to optimal msvc implements.
Productivity is a state of mind.
User avatar
revelator
 
Posts: 2605
Joined: Thu Jan 24, 2008 12:04 pm
Location: inside tha debugger

Previous

Return to Engine Programming

Who is online

Users browsing this forum: No registered users and 2 guests