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

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

 



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
--
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