[9] RFR(L): 8037816 : Fix for 8036122 breaks build with Xcode5/clang

David Chase david.r.chase at oracle.com
Fri Apr 18 17:17:55 UTC 2014


On 2014-04-17, at 8:01 PM, Christian Thalinger <christian.thalinger at oracle.com> wrote:
>>>> (3) In many cases, the best fix is to replace a call to a method expecting a format with one to a method expecting only a string.  That is, replace “print(s)” with “print_raw(s)”.  Note that when there is a single parameter this cannot possibly be a mistake.
>>> 
>>> Yes, it fixes the problem but it will be difficult to remember or know (for new people) that you should use print_raw instead.
>> 
>> What's the alternative?  Once we get our compilers up to decent revisions, the checking will just be there, and
>> anyone who writes print(s) will get an error.
> 
> Not sure.  Can we make:
> 
>    void print_raw(const char* str)            { write(str, strlen(str)); }
> 
> just
> 
>    void print(const char* str)            { write(str, strlen(str)); }
> 
> ?  That would help a lot, I think.

I'm planning to make (and test) the following changes, which I've run past multiple
compilers already in a test program.  Assuming all goes well, there will be a new webrev
in a while.  HOWEVER, I could not figure out how to overload two definitions of "print"
when one of them is fmt + varargs and the other is just a string.  I get ambiguous overloading
errors like this:

Test print(const char * s) overloading.
test.cpp:30:3: error: call to 'my_print' is ambiguous
  my_print(s);
  ^~~~~~~~
test.cpp:12:6: note: candidate function
void my_print(const char * fmt, ...) {
     ^
test.cpp:18:6: note: candidate function
void my_print(const char * s) {
     ^

So unless someone has a better idea, we're stuck with print/print_raw, and it would be
a good idea to run the compiler with -Wformat-security to avoid mistakes.  I'm not quite
clear on why gcc was not also reporting these errors already in our builds, since it is
able to do so and it looked like the flag was set that enabled them.

Anyone have a better idea?

---------------------------
Changes to fix:
Replace const char * fmt = string with #define fmt string to work around literal-minded format-checking.

Test push/pop with older versions of clang (works).

Add _Pragma("GCC diagnostic ignored \"-Wformat-security\"") for older clang.

P2I inline method.
static inline intptr_t p2i(void * p) { return (intptr_t) p; }

---------------------------



More information about the hotspot-dev mailing list