String handling in C

Started by
31 comments, last by cr88192 11 years, 1 month ago
In a former colleague's code about 14 years ago:

sprintf(str, "%s %s %s", a, b);

The value of str was displayed on screen after this. This had been live at a bank for a couple of years before it was discovered. The reason it took so long to notice is that the value on the stack that was used for the third string's address happened to point to a byte containing zero.
Advertisement

Does any C or even C++ compiler catch a mismatch like that? That's a terrible bug to have. Code reviews FTW.

Beginner in Game Development?  Read here. And read here.

 

gcc looks at the format string for printf & co and gives a warning I think. It has to be built in to the compiler (or via metadata related to a function declaration) since using variable length argument lists removes all checking to do with type and number of arguments...

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

"String handling in C" is a coding horror all on it's own - no further comment is necessary.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

Care to elaborate? :D

I purposely stayed away from C's formatted output for the brief time I was learning C++.

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

My journals: dustArtemis ECS framework and Making a Terrain Generator

Could be worse, could be a web of pointers so convoluted that they point to nothing while trying to point to some embedded function, with an over-called string in it that still works for some reason. *shudders*

Editor // Joy-Toilet.com

Anything But Shitty Entertainment!

gcc looks at the format string for printf & co and gives a warning I think. It has to be built in to the compiler (or via metadata related to a function declaration) since using variable length argument lists removes all checking to do with type and number of arguments...

Clang also does this, plus, it also checks that the format string is correct with respect to argument types. Really handy!

Care to elaborate? biggrin.png

I purposely stayed away from C's formatted output for the brief time I was learning C++.

You can overflow the buffer at any time, you don't know how long it is, you need to walk over the entire string in order to do any operation (which can easily lead to O(n2) algorithms) - strings in C basically contain everything that one should not do if one was going to design a string library. See http://en.wikipedia.org/wiki/C_string_handling#Criticism and http://www.joelonsoftware.com/articles/fog0000000319.html for more.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

Does any C or even C++ compiler catch a mismatch like that? That's a terrible bug to have. Code reviews FTW.


This was an IBM C compiler which didn't perform any such checks. I don't think any did at the time.

The team were supposed to do code reviews and should have picked this up then. My job was developer support which included solving "our code's crashing and we don't know why" type problems. In this case I was given a memory dump and asked to figure out what was going wrong.

Care to elaborate? :D

I purposely stayed away from C's formatted output for the brief time I was learning C++.

Each %s in the string means there should be another parameter that is a pointer to a string. The line should look like:

sprintf(str, "%s %s %s", a, b, c);
Because the function is expecting another parameter on the stack to go with the third %s, it will use whatever is in the next memory location after the b. This could be anything!

This topic is closed to new replies.

Advertisement