petzval / btferret

Python and C Bluetooth Library
MIT License
122 stars 21 forks source link

Compilation error with gcc 11.4.0 #29

Closed elfrances closed 7 months ago

elfrances commented 1 year ago

Compilation of btferret following the instructions in the README results in the following two errors:

mmourier@optiplex:~/BLE/btferret$ gcc btferret.c btlib.c -o btferret
btferret.c: In function ‘getstring’:
btferret.c:1777:5: warning: format not a string literal and no format arguments [-Wformat-security]
 1777 |     printf(prompt);
      |     ^~~~~~
btlib.c: In function ‘localctics’:
btlib.c:2047:36: warning: ' ' flag used with ‘%o’ gnu_printf format [-Wformat=]
 2047 |         NPRINT "%shandle must be % or greater\n",errs,starthandle);
      |                                    ^

The fix is pretty straightforward, as shown by the diff's below:

mmourier@optiplex:~/BLE/btferret$ git diff
diff --git a/btferret.c b/btferret.c
index bec3d51..15e442c 100644
--- a/btferret.c
+++ b/btferret.c
@@ -1774,7 +1774,7 @@ void getstring(char *prompt,char *s,int len)

   do
     {
-    printf(prompt);
+    printf("%s",prompt);
     fgets(s,len,stdin);
     }
   while(s[0] == 10);
diff --git a/btlib.c b/btlib.c
index 8b07ad3..e62a10d 100644
--- a/btlib.c
+++ b/btlib.c
@@ -2044,7 +2044,7 @@ int localctics(int starthandle)
       {
       if(cp->chandle < starthandle)
         {
-        NPRINT "%shandle must be % or greater\n",errs,starthandle);
+        NPRINT "%s handle must be %d or greater\n",errs,starthandle);
         return(0);
         }

And when compilation warnings are enabled, gcc prints lots of them, many related to mixing up pointers of different sign: e.g. "char " and "unsigned char ". I think it would be "a good thing" if the code could compile warning-free ...

petzval commented 1 year ago

Thanks for spotting the error in btlib. I've uploaded a fix. i have not changed the btferret example because prompt will be a valid string - the compiler cannot be expected to see it. You make a good point about the warnings.