* How to check if char pointer is a constant!?
@ 2016-07-07 19:40 Daniel.
2016-07-07 20:26 ` Phil Sutter
0 siblings, 1 reply; 5+ messages in thread
From: Daniel. @ 2016-07-07 19:40 UTC (permalink / raw)
To: linux-c-programming@vger.kernel.org
Hi everybody,
I have some function, say
void empty(char *p) { p[0] = '\0'; }
If I call it like this empty("Hello") it will segfaults since "Hello"
is put in readonly section of the program. Is there a way to check for
this?! Maybe some nasty gcc extension!?
Regards,
--
"Do or do not. There is no try"
Yoda Master
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: How to check if char pointer is a constant!?
2016-07-07 19:40 How to check if char pointer is a constant!? Daniel.
@ 2016-07-07 20:26 ` Phil Sutter
2016-07-08 12:33 ` Daniel.
0 siblings, 1 reply; 5+ messages in thread
From: Phil Sutter @ 2016-07-07 20:26 UTC (permalink / raw)
To: Daniel.; +Cc: linux-c-programming@vger.kernel.org
Hi,
On Thu, Jul 07, 2016 at 04:40:56PM -0300, Daniel. wrote:
> I have some function, say
> void empty(char *p) { p[0] = '\0'; }
>
> If I call it like this empty("Hello") it will segfaults since "Hello"
> is put in readonly section of the program. Is there a way to check for
> this?! Maybe some nasty gcc extension!?
The problem here is upon calling the function, the const char *
parameter is implicitly casted to char *. So inside the function there
is no way to check this I'd say. You can call gcc with -Wwrite-strings
to have it generate a warning whenever a string constant is assigned to
a non-const char * variable.
HTH, Phil
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: How to check if char pointer is a constant!?
2016-07-07 20:26 ` Phil Sutter
@ 2016-07-08 12:33 ` Daniel.
2016-07-08 12:59 ` Phil Sutter
0 siblings, 1 reply; 5+ messages in thread
From: Daniel. @ 2016-07-08 12:33 UTC (permalink / raw)
To: Phil Sutter, Daniel., linux-c-programming@vger.kernel.org
Hi all thanks for the reply.
I think I wasn't clear. Here is a working example: http://codepad.org/0SUG6IcQ
The function get_pair_from_string() should I) remove trailing spcaes, II) return
the address for key and value using it's last parameters. The problem is that
since I modify the first parameter calling with strings constants generates a
segmentation fault. This is because strings constans aren't modifiable. I wan't
to have better hints when this happen than just "SEGSEGV". I think that the
-Wwrite-strings is what I was looking for :D, thanks Phil!!!
John, I'll be in production, but not at this time. Luckly, that
function is file scoped
so I don't have to bother that much. Still I like to avoid the crypt
"SEGSEGV" error
message as much as possible. Usually I assert for NULL pointers and give a nice
error message but this case is trickier. I'll take Phil advice and use
-Wwrite-strings
Thanks everybody!
Regards,
2016-07-07 17:26 GMT-03:00 Phil Sutter <phil@nwl.cc>:
> Hi,
>
> On Thu, Jul 07, 2016 at 04:40:56PM -0300, Daniel. wrote:
>> I have some function, say
>> void empty(char *p) { p[0] = '\0'; }
>>
>> If I call it like this empty("Hello") it will segfaults since "Hello"
>> is put in readonly section of the program. Is there a way to check for
>> this?! Maybe some nasty gcc extension!?
>
> The problem here is upon calling the function, the const char *
> parameter is implicitly casted to char *. So inside the function there
> is no way to check this I'd say. You can call gcc with -Wwrite-strings
> to have it generate a warning whenever a string constant is assigned to
> a non-const char * variable.
>
> HTH, Phil
--
"Do or do not. There is no try"
Yoda Master
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: How to check if char pointer is a constant!?
2016-07-08 12:33 ` Daniel.
@ 2016-07-08 12:59 ` Phil Sutter
2016-07-08 14:01 ` Daniel.
0 siblings, 1 reply; 5+ messages in thread
From: Phil Sutter @ 2016-07-08 12:59 UTC (permalink / raw)
To: Daniel.; +Cc: linux-c-programming@vger.kernel.org
On Fri, Jul 08, 2016 at 09:33:03AM -0300, Daniel. wrote:
> Hi all thanks for the reply.
>
> I think I wasn't clear. Here is a working example: http://codepad.org/0SUG6IcQ
>
> The function get_pair_from_string() should I) remove trailing spcaes, II) return
> the address for key and value using it's last parameters. The problem is that
> since I modify the first parameter calling with strings constants generates a
> segmentation fault. This is because strings constans aren't modifiable. I wan't
> to have better hints when this happen than just "SEGSEGV". I think that the
> -Wwrite-strings is what I was looking for :D, thanks Phil!!!
Glad I could help.
> John, I'll be in production, but not at this time. Luckly, that
> function is file scoped
> so I don't have to bother that much. Still I like to avoid the crypt
> "SEGSEGV" error
> message as much as possible. Usually I assert for NULL pointers and give a nice
> error message but this case is trickier. I'll take Phil advice and use
> -Wwrite-strings
Calling 'SIGSEGV' an error message is a slight underestimation. Actually
your program simply crashes. The gcc warning to activate will just help
to keep your code sane, not improve run-time behaviour.
If you want to modify strings no matter whether they are constant or
not, strdup() them into a local variable and work with that. Especially
since it's just for parsing as your example shows. Just keep in mind
that you will have to free the duplicated string at some point,
otherwise you will have a memory leak (which at some point will lead to
an equally cryptic message as your segfault did).
Cheers, Phil
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: How to check if char pointer is a constant!?
2016-07-08 12:59 ` Phil Sutter
@ 2016-07-08 14:01 ` Daniel.
0 siblings, 0 replies; 5+ messages in thread
From: Daniel. @ 2016-07-08 14:01 UTC (permalink / raw)
To: Phil Sutter, Daniel., linux-c-programming@vger.kernel.org
Hi Phil,
Yes I could use strduped strings to avoid this case but at this point
I just want to
have sure that if I do that mistake in future (trying to change a
const str *) the compiler at
last warns me about it :). In the real case this function is called
with a buffer from a readed
file so that it's first parameter aways point to valid lvalue data,
but I (or other) may change the code
in future and try to call it with string constants, I would like to
prevent my self from doing
that and if it happen have a better clue of what happen. Your anwser
apply just as I expected.
Cheers,
2016-07-08 9:59 GMT-03:00 Phil Sutter <phil@nwl.cc>:
> On Fri, Jul 08, 2016 at 09:33:03AM -0300, Daniel. wrote:
>> Hi all thanks for the reply.
>>
>> I think I wasn't clear. Here is a working example: http://codepad.org/0SUG6IcQ
>>
>> The function get_pair_from_string() should I) remove trailing spcaes, II) return
>> the address for key and value using it's last parameters. The problem is that
>> since I modify the first parameter calling with strings constants generates a
>> segmentation fault. This is because strings constans aren't modifiable. I wan't
>> to have better hints when this happen than just "SEGSEGV". I think that the
>> -Wwrite-strings is what I was looking for :D, thanks Phil!!!
>
> Glad I could help.
>
>> John, I'll be in production, but not at this time. Luckly, that
>> function is file scoped
>> so I don't have to bother that much. Still I like to avoid the crypt
>> "SEGSEGV" error
>> message as much as possible. Usually I assert for NULL pointers and give a nice
>> error message but this case is trickier. I'll take Phil advice and use
>> -Wwrite-strings
>
> Calling 'SIGSEGV' an error message is a slight underestimation. Actually
> your program simply crashes. The gcc warning to activate will just help
> to keep your code sane, not improve run-time behaviour.
>
> If you want to modify strings no matter whether they are constant or
> not, strdup() them into a local variable and work with that. Especially
> since it's just for parsing as your example shows. Just keep in mind
> that you will have to free the duplicated string at some point,
> otherwise you will have a memory leak (which at some point will lead to
> an equally cryptic message as your segfault did).
>
> Cheers, Phil
--
"Do or do not. There is no try"
Yoda Master
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2016-07-08 14:01 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-07-07 19:40 How to check if char pointer is a constant!? Daniel.
2016-07-07 20:26 ` Phil Sutter
2016-07-08 12:33 ` Daniel.
2016-07-08 12:59 ` Phil Sutter
2016-07-08 14:01 ` Daniel.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).