On 19/08/2007 00:04, Nico Weber wrote: > Hi, > > On Aug 18, 2007, at 5:00 PM, Bram Moolenaar wrote: >> Problem: Compiler warnings for using "const char *" for "char *". >> Solution: Add type casts. (Chris Sutcliffe) > > at least in C++. this is a very dangerous patch. If you cast a const > char* to a char* and then modify the char*, you get undefined > behaviour (I guess this is not a problem now, but if searchpath() is > changed to modify its argument array in the future, then this will > introduce a bug). I don't know if this is true in C as well, does > someone know?
Yep, the effect of modifying a string literal is undefined behaviour. In general, casting in C is lying to your compiler and bad things can happen. (The only valid case I can think of is casting reals to integers - assuming you have done appropriate range checks of course ;-). The C standard is quite clear that the effective array elements of a string liternal have type char, not const char. For some discussion (particularly the rationale) see: http://groups.google.co.uk/group/comp.os.linux.development.system/browse_thread/thread/388a8e7b308ef8ad/a8c037a4f2787621?lnk=st&q=gcc+4+string+literal+%22const+char%22+ansi&rnum=2&hl=en#a8c037a4f2787621 http://groups.google.co.uk/group/comp.lang.c/browse_thread/thread/deafe0a166b401bd/55f03162aecc875c?lnk=st&q=gcc+4+string+literal+%22const+char%22+ansi&rnum=4&hl=en#55f03162aecc875c It looks like a change with more recent version of gcc, perhaps the builds with recent gcc need to have -fwritable-strings set. TTFN Mike -- Altruism is a fine motive, but if you want results, greed works much better. --~--~---------~--~----~------------~-------~--~----~ You received this message from the "vim_dev" maillist. For more information, visit http://www.vim.org/maillist.php -~----------~----~----~----~------~----~------~--~---
