3
votes

We cleaned up a large number of warnings. In several cases, we replaced %s with %d. Everything seemed OK until we realized that the generated code wouldn't compile - strings had been replaced by numbers.

NOTE: I had the wrong revision checked out when I did the copy&paste. The fprintf string has been edited!

GCC output

fedex_plus/classes.c:2425:3: warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘int’ [-Wformat]

lines 2424 and 2425:

fprintf(file, "\n//\t%s_ptr create_TIE();\n\tIDL_Application_instance_ptr create_TIE();\n", 
    ENTITYget_CORBAname(entity));

function ENTITYget_CORBAname:

const char *
ENTITYget_CORBAname (Entity ent)
{
    static char newname [BUFSIZ];
    strcpy( newname, ENTITYget_name(ent) );
    newname[0] = ToUpper (newname [0]);
    return newname;
}
4
ouch for strcpy... This is way more serious than printing a random number here or there. You should consider strncpy instead.Alexandre C.
@Alexandre: Thanks for that. We're going to run cppcheck on it at some point; I assume that this type of problem will be caught. Right now we're trying to get rid of gcc warnings to see if they fix any of the bugs we know of. The code is so old that the documentation explains in detail how to use the mouse (github.com/mpictor/StepClassLibrary/wiki/dataprobe.1)Mark
omg... Also good luck with the 3000+ line source file !Alexandre C.
@Alexandre: 5350 lines is better than the 310089 lines in one of the .cc files it generates for STEP AP214! The ESA developed a similar tool they haven't released, and there are insanely expensive commercial tools - unfortunately, this is the only open source tool of its kind.Mark

4 Answers

11
votes

My bet is that no declaration of ENTITYget_CORBAname is available at the fprintf spot. Should this be the case, it defaults to the implicit signature:

int ENTITYget_CORBAname(...);

hence the warning about the result type being an int.

Check your code with -Wimplicit (implied by -Wall), or try to put

const char *ENTITYget_CORBAname (Entity ent);

before the fprintf line and check if the warning disappears. If it does, then you're likely missing a header.

1
votes

If you want to print a pointer value, use %p, if you want to print the string returned from the function, use %s. Don't use %d in any of this cases. See here.

1
votes

I assume that ENTITY_GETCorbaName() returns a string, so if you want to print that, use %s. If you want to print it as pointer, use %p.

0
votes

use %s in your printf to print that string or use %p in your printf to show pointer value you can also use %lld to prints its value