trinity-devel@lists.pearsoncomputing.net

Message: previous - next
Month: November 2012

Re: [trinity-devel] tdewebdev: warning: comparison with string literal

From: Darrell Anderson <humanreadable@...>
Date: Thu, 1 Nov 2012 17:12:54 -0700 (PDT)
> You reversed the sense of comparison,
> 
> strcmp(w->name(), args[0]) meaning "is not equal".
> 
> Correct: 
> 
> if (0 == strcmp(w->name(), args[0]) && w->className() == "PopupMenu")
> 
> In any case, this seems a workaround. I guess that args[0] is TQString, so the
> comparison should be valid. Try to reverse order of arguments, like:
> 
> if (args[0] == w->name() && w->className() == "PopupMenu")

Thank you much for coaching. Very much! :)

My original proposed patch failed because I received the same warning message with the patch. The problematic comparison causing the warning message is the second part of the AND, not the first part. My original proposed patch:

-        if (w->name() == args[0] && w->className() == "PopupMenu")
+        if (strcmp(w->name(), args[0]) && w->className() == "PopupMenu")

My new patch:

-        if (w->name() == args[0] && w->className() == "PopupMenu")
+        if (w->name() == args[0] && strcmp(w->className(), "PopupMenu"))

That revision resulted in no warning message during the build. However, after reading your explanation I now am concerned that the patch is incorrect.

From what I have read, in C++ the strcmp function returns three possible values. If I understand correctly, the strcmp function returns zero when the two strings are equal, a -1 when the first string is alphabetically "less than" the first string and a positive value when the first string is alphabetically "greater than" the second string.

AND statements are used to produce only one true result: when both condition A and condition B are true. In our tdewebdev code the only time condition B [ w->className() == "PopupMenu" ] is true is when the strcmp result is compared to a value. As I don't yet read C++ code very well, I don't know the intent of the code, but I'm guessing the following is the intent:

-        if (w->name() == args[0] && w->className() == "PopupMenu")
+        if (w->name() == args[0] && ( strcmp(w->className(), "PopupMenu")) == 0 )

Does that look correct? :)

Darrell