Share

Flawfinder

Tracker: Feature Requests

5 give warning: Local variables declared const are mutable! - ID: 1857140
Last Update: Tracker Item Submitted ( klaus_trainer )

I just came across this issue on Ulrich Drepper's blog
(http://udrepper.livejournal.com/15119.html):

#include <stdio.h>
#include <string.h>
int main(void)
{
const char s[] = "hello";
strcpy (s, "bye");
puts (s);
return 0;
}

gcc will compile it, albeit with a warning.
The executable's output will be "bye".
As long as a character constant will be declared in a function using the
braces ("[]") it won't be immutable even though many people would consider
it to be.

A better solution:

static const char s[] = "hello";

Now trying to write to s results immediately in a crash at runtime. This
flaw can be detected more easily when testing and even though it won't be
detected there is one chance less to compromise.

I'd like flawfinder to give an appropriate warning message with an
improvement proposal which could be e.g.:

Consider using both static and const modifiers together to prevent
unintended alterations of variables declared as const.


Klaus Trainer ( klaus_trainer ) - 2007-12-23 19:17

5

Open

None

Nobody/Anonymous

None

None

Public


Comments




Log in to comment.

No follow-up comments have been posted.

Attached File

No Files Currently Attached

Change

No changes have been made to this artifact.