[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/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/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/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/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/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 - HUD coding question...

HUD coding question...

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

Moderator: InsideQC Admins

HUD coding question...

Postby ScatterBox » Wed Oct 23, 2013 8:53 pm

Last edited by ScatterBox on Wed Oct 23, 2013 10:18 pm, edited 2 times in total.
User avatar
ScatterBox
 
Posts: 50
Joined: Sun Oct 13, 2013 7:53 pm

Re: HUD coding question...

Postby Spike » Wed Oct 23, 2013 9:54 pm

it repeats itself needlessly (bad coding style)).
indentation is fucked (insulting to anyone who you ask for help).
it has a (repeated) bug (bad.. err... bugs).
you use sprintf (bad habit).
.
Spike
 
Posts: 2914
Joined: Fri Nov 05, 2004 3:12 am
Location: UK

Re: HUD coding question...

Postby ScatterBox » Wed Oct 23, 2013 10:17 pm

User avatar
ScatterBox
 
Posts: 50
Joined: Sun Oct 13, 2013 7:53 pm

Re: HUD coding question...

Postby Spike » Wed Oct 23, 2013 10:35 pm

regarding sprintf, you'd get into a much better habit from using snprintf (or equivelent) instead.
the bug would be apparent if you tried running it.
indenting is important, though I realise that pasting it into a browser can mess it up for you.
.
Spike
 
Posts: 2914
Joined: Fri Nov 05, 2004 3:12 am
Location: UK

Re: HUD coding question...

Postby ScatterBox » Wed Oct 23, 2013 10:54 pm

User avatar
ScatterBox
 
Posts: 50
Joined: Sun Oct 13, 2013 7:53 pm

Re: HUD coding question...

Postby Spike » Thu Oct 24, 2013 1:30 am

%i/%i is two arguments. but you only provide one. the first number printed have the value, but the second will be pseudo-random (whatever value was in memory at the time).

sprintf does not bound the string, thus (C console program):
char buf1[64];
char buf2[64];
puts("Please enter your name!\n");
fgets(buf1, sizeof(buf1), stdin);
sprintf(buf2, "Hello there %s, I hope you are well\n", buf1);
puts(buf2);
will write past the end of buf2. At the end of buf2 you will presumably find the return address of your function. If the values are carefully crafted, you can insert some exploit code into those 63 bytes that are writable in buf1 and overwrite the return address to point to that data. you just have to avoid nulls. :P

Now admittedly if you'd used snprintf instead, it wouldn't have been formatted properly (no trailing part of the string, including the newline), but at least it wouldn't be exploitable (normally).

if you're unable to compile code that uses snprintf, you must be using windows. Sorry, but microsoft are morons.
On windows, you'll need to use _snprintf instead. Be warned that unlike linux's snprintf function, it does not guarentee null termination. Which means you need to reduce the number of bytes by 1, and unconditionally write the last byte of the buffer as 0. Which is a bit of a pain. It sucks to use windows. You can fairly easily write a wrapper function for it, but its annoying to have to do that. If you're lazy you can #ifdef _WIN32 #define snprintf sprintf_s #endif but your program will safely abort if its too long (which can be a major issue in server code, yay for denial of service attacks). So really, there's no choice but to validate all string lengths carefully... Programming can be a real pain some times.
In your original case, truncation is fine and dandy. Yay for snprintf.
.
Spike
 
Posts: 2914
Joined: Fri Nov 05, 2004 3:12 am
Location: UK

Re: HUD coding question...

Postby ScatterBox » Sat Oct 26, 2013 5:41 pm

User avatar
ScatterBox
 
Posts: 50
Joined: Sun Oct 13, 2013 7:53 pm

Re: HUD coding question...

Postby frag.machine » Sun Oct 27, 2013 5:31 am

If you're programming for Windows with Visual Studio (likely), try sprintf_s(). It's similar to sprintf, but takes a extra parameter for the buffer size.
I know FrikaC made a cgi-bin version of the quakec interpreter once and wrote part of his website in QuakeC :) (LordHavoc)
User avatar
frag.machine
 
Posts: 2120
Joined: Sat Nov 25, 2006 1:49 pm

Re: HUD coding question...

Postby ScatterBox » Sun Oct 27, 2013 5:30 pm

User avatar
ScatterBox
 
Posts: 50
Joined: Sun Oct 13, 2013 7:53 pm


Return to Engine Programming

Who is online

Users browsing this forum: No registered users and 2 guests