how to fix brp and rpmlint warnings – today:
Expression compares a char* pointer with a string literal

After having seen this kind of question several times, I think it could be interesting to start creating a small ‘series of rpmlint errors and their typical fixes”.

Interested in it? So welcome to part #1 (this issue). The “series” is not really fledged out, but I will try to write post for various errors / topics, as they come along. Just drop me a note with an error you have seen / experienced in OBS while building a package.

So what does it mean, if OBS tells you this:
Expression compares a char* pointer with a string literal. Usually a strcmp() was intended by the programmer

Let’s have a look at a very simple C program, to show the issue:
#include <stdio.h>

int main() {
char *mytext;
mytext = "Help";
if (mytext == "Help") {
printf("This should raise a warning");
}
return 0;
}
Trying to compile this very simple C program, which on first sight many would probably say this is correct, will result in this:

$ gcc test1.c -Wall
test1.c: In function ‘main’:
test1.c:6:14: warning: comparison with string literal results in unspecified behavior

So, even though we had such an easy program, line 6 seems to be an issue for us:

if (mytext == “Help”) {

The issue, as simple as it may sound, is the comparison of a pointer (char *) with a literal (“Help”).

So how do we fix it? It involves changing the code, thus writing a patch (there are many good helps around for this… I won’t dive into patching here).. Of course, after having written a patch, be a good downstream, send it upstream!
What you want to do here is to replace this comparison with strcmp (just as OBS’ error message implied).
Look at this code now:
#include <stdio.h>
#include <string.h>

int main() {
char *mytext;
mytext = "Help";
if (strcmp(mytext,"Help")==0) {
printf("No warning here\n");
}
return 0;
}
I changed “mytext == “Help” to strcmp(mtext,”Help”)==0 (man 3 strcmp will explain you why… 0 means the texts are equal, or ‘0 difference detected’ can help you remember that). As the code was too simple and strcmp was not yet defined (this of course can also happen in packages you patch), I had to add an additional #include <string.h>.

Not too hard, is it?

So what do you think? Such a series of “how to fix my typical brp / lint warnings” useful?

7 responses to “how to fix brp and rpmlint warnings – today:
Expression compares a char* pointer with a string literal”

  1. Lazy_Kent Avatar
    Lazy_Kent

    Very useful! Thanks.

  2. Malcolm Avatar
    Malcolm

    Hi
    Excellent, look forward to more of these 🙂

  3. Atri Avatar
    Atri

    Thank you; such a series will be a big help.

  4. RedDwarf Avatar
    RedDwarf

    The wrong example could be

    #include

    int main() {
    const char mytext[] = “Help”;
    if (mytext == “Help”) {
    printf(“This should raise a warning”);
    }
    return 0;
    }

    To avoid making Ulrich Drepper cry 😉
    http://www.akkadia.org/drepper/dsohowto.pdf section 2.4.1

    But sure, the series is good to have. And is badly needed for some esoteric topics as strict aliasing.

  5. crrodriguez Avatar
    crrodriguez

    Note that strcmp is easily misused and will certainly lead to bugs . I suggest you define a macro like

    #define streq(a,b) (strcmp((a),(b)) == 0)

    ..or an inline function,as macros are pretty evil 😉

    Keep going!

  6. Dominique Leuenberger Avatar

    Sure.. but: do we want to have our packager army introduce the most sophisticated and complex code into any upstream project?
    The target audience for his series is mostly packagers. Some of them are coders, many are not, some would like to learn, some don’t care and give up.

    It’s nice to point out nice nifty tricks. But honestly, I doubt we would achieve a lot of packagers taking care of the errors, if it goes too far.

  7. Bruno Friedmann Avatar
    Bruno Friedmann

    Yeah years after this is still helpful 🙂
    Thanks for the post.