{"id":151,"date":"2011-03-24T23:29:55","date_gmt":"2011-03-24T22:29:55","guid":{"rendered":"http:\/\/dominique.leuenberger.net\/blog\/?p=151"},"modified":"2011-03-24T23:30:30","modified_gmt":"2011-03-24T22:30:30","slug":"how-to-fix-brp-and-rpmlint-warnings-today-expression-compares-a-char-pointer-with-a-string-literal","status":"publish","type":"post","link":"https:\/\/dominique.leuenberger.net\/blog\/2011\/03\/how-to-fix-brp-and-rpmlint-warnings-today-expression-compares-a-char-pointer-with-a-string-literal\/","title":{"rendered":"how to fix brp and rpmlint warnings &#8211; today:<br\/> Expression compares a char* pointer with a string literal"},"content":{"rendered":"<p>After having seen this kind of question several times, I think it could be interesting to start creating a small &#8216;series of rpmlint errors and their typical fixes&#8221;.<\/p>\n<p>Interested in it? So welcome to part #1 (this issue). The &#8220;series&#8221; 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.<\/p>\n<p>So what does it mean, if OBS tells you this:<br \/>\nExpression compares a char* pointer with a string literal. Usually a strcmp() was intended by the programmer<\/p>\n<p>Let&#8217;s have a look at a very simple C program, to show the issue:<br \/>\n<code>#include &lt;stdio.h&gt;<\/p>\n<p>int main() {<br \/>\n  char *mytext;<br \/>\n  mytext = \"Help\";<br \/>\n  if (mytext == \"Help\") {<br \/>\n    printf(\"This should raise a warning\");<br \/>\n  }<br \/>\n  return 0;<br \/>\n}<\/code>Trying to compile this very simple C program, which on first sight many would probably say this is correct, will result in this:<\/p>\n<blockquote><p>\n$ gcc test1.c -Wall<br \/>\ntest1.c: In function \u2018main\u2019:<br \/>\ntest1.c:6:14: warning: comparison with string literal results in unspecified behavior\n<\/p><\/blockquote>\n<p>So, even though we had such an easy program, line 6 seems to be an issue for us:<\/p>\n<blockquote><p>if (mytext == &#8220;Help&#8221;) {<\/p><\/blockquote>\n<p>The issue, as simple as it may sound, is the comparison of a pointer (char *) with a literal (&#8220;Help&#8221;).<\/p>\n<p>So how do we fix it? It involves changing the code, thus writing a patch (there are many good helps around for this&#8230; I won&#8217;t dive into patching here).. Of course, after having written a patch, be a good downstream, send it upstream!<br \/>\nWhat you want to do here is to replace this comparison with strcmp (just as OBS&#8217; error message implied).<br \/>\nLook at this code now:<br \/>\n<code>#include &lt;stdio.h&gt;<br \/>\n#include &lt;string.h&gt;<\/p>\n<p>int main() {<br \/>\n  char *mytext;<br \/>\n  mytext = \"Help\";<br \/>\n  if (strcmp(mytext,\"Help\")==0) {<br \/>\n    printf(\"No warning here\\n\");<br \/>\n  }<br \/>\n  return 0;<br \/>\n}<\/code>I changed <em>&#8220;mytext == &#8220;Help&#8221;<\/em> to <em>strcmp(mtext,&#8221;Help&#8221;)==0<\/em> (<a href=\"http:\/\/linux.die.net\/man\/3\/strcmp\">man 3 strcmp<\/a> will explain you why&#8230; 0 means the texts are equal, or &#8216;0 difference detected&#8217; 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 <em>#include &lt;string.h&gt;<\/em>.<\/p>\n<p>Not too hard, is it?<\/p>\n<p>So what do you think? Such a series of &#8220;how to fix my typical brp \/ lint warnings&#8221; useful?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>After having seen this kind of question several times, I think it could be interesting to start creating a small &#8216;series of rpmlint errors and their typical fixes&#8221;. Interested in it? So welcome to part #1 (this issue). The &#8220;series&#8221; is not really fledged out, but I will try to write post for various errors [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[24,3],"tags":[25],"class_list":["post-151","post","type-post","status-publish","format-standard","hentry","category-lint-brp-series","category-opensuse","tag-obs"],"_links":{"self":[{"href":"https:\/\/dominique.leuenberger.net\/blog\/wp-json\/wp\/v2\/posts\/151","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dominique.leuenberger.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dominique.leuenberger.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dominique.leuenberger.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dominique.leuenberger.net\/blog\/wp-json\/wp\/v2\/comments?post=151"}],"version-history":[{"count":14,"href":"https:\/\/dominique.leuenberger.net\/blog\/wp-json\/wp\/v2\/posts\/151\/revisions"}],"predecessor-version":[{"id":177,"href":"https:\/\/dominique.leuenberger.net\/blog\/wp-json\/wp\/v2\/posts\/151\/revisions\/177"}],"wp:attachment":[{"href":"https:\/\/dominique.leuenberger.net\/blog\/wp-json\/wp\/v2\/media?parent=151"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dominique.leuenberger.net\/blog\/wp-json\/wp\/v2\/categories?post=151"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dominique.leuenberger.net\/blog\/wp-json\/wp\/v2\/tags?post=151"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}