Re: How to check if char pointer is a constant!?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxx>:
> 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
--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html



[Index of Archives]     [Linux Assembler]     [Git]     [Kernel List]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [C Programming]     [Yosemite Campsites]     [Yosemite News]     [GCC Help]

  Powered by Linux