Welcome to the Builder Academy

Question Need guidance on fixing one of the compiler warnings

More
22 Dec 2024 01:47 #10484 by gbcs
I downloaded the latest version from GitHub and compiled it. It gives a few warning but compiles fine and runs.
I am compiling on Ubuntu 22.04.5 and using GCC version 11.4.0

I decided to fix the compile warnings and come across one that I am not sure on.

Here is the output of the compile:

aedit.c: In function ‘aedit_parse’:
aedit.c:558:7: warning: this ‘if’ clause does not guard... [-Wmisleading-indentation]
  558 |      if (OLC_ACTION(d)->command)
      |      ^~
In file included from aedit.c:16:
oasis.h:135:24: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the ‘if’
  135 | #define OLC_ACTION(d)  (OLC(d)->action)  /**< Action structure    */
      |                        ^
aedit.c:560:9: note: in expansion of macro ‘OLC_ACTION’
  560 |        OLC_ACTION(d)->command = strdup(arg);
      |        ^~~~~~~~~~

Here is the code from aedit.c

  case AEDIT_ACTION_NAME:
        ..
        ..
        ..
        ..
 
  Line 558        if (OLC_ACTION(d)->command)
                           free(OLC_ACTION(d)->command);
  Line 560           OLC_ACTION(d)->command = strdup(arg);

                  break;

I noticed that further down the code there is a similar case statement. 

Here is that code.

  case AEDIT_SORT_AS:
      if (!*arg || strchr(arg,' ')) {
        aedit_disp_menu(d);
        return;
      }
      if (OLC_ACTION(d)->sort_as) {
        free(OLC_ACTION(d)->sort_as);
        OLC_ACTION(d)->sort_as = strdup(arg);
      }
      break;

So in one of the case statements the move from strdup(arg) is not protected by the if statement and in the other it is protected. 

Is it that the two actions are correctly handled differently and it is just an indentation problem? Or is the first case statement wrong and should have braces protecting the move?  

Graham

Please Log in or Create an account to join the conversation.

More
05 Jan 2025 19:14 - 05 Jan 2025 19:14 #10507 by thomas
There is indeed a bug here. These are the lines as they are in the current HEAD commit:
Code:
    case AEDIT_ACTION_NAME:       if (!*arg || strchr(arg,' ')) {         aedit_disp_menu(d);         return;       }       if (OLC_ACTION(d)->command)         free(OLC_ACTION(d)->command);         OLC_ACTION(d)->command = strdup(arg);       break;     case AEDIT_SORT_AS:       if (!*arg || strchr(arg,' ')) {         aedit_disp_menu(d);         return;       }       if (OLC_ACTION(d)->sort_as) {         free(OLC_ACTION(d)->sort_as);         OLC_ACTION(d)->sort_as = strdup(arg);       }       break;
As it is, sort_as can never be set. The correct version should be:
Code:
    case AEDIT_ACTION_NAME:       if (!*arg || strchr(arg,' ')) {         aedit_disp_menu(d);         return;       }       if (OLC_ACTION(d)->command)         free(OLC_ACTION(d)->command);       OLC_ACTION(d)->command = strdup(arg);       break;     case AEDIT_SORT_AS:       if (!*arg || strchr(arg,' ')) {         aedit_disp_menu(d);         return;       }       if (OLC_ACTION(d)->sort_as)         free(OLC_ACTION(d)->sort_as);       OLC_ACTION(d)->sort_as = strdup(arg);           break;
The point being, if sort_as is NULL, no strdup call will happen in the original version.
Note also, that I removed two spaces from the "OLC_ACTION(d)->command = strdup(arg);" line to silence the linter :)
Last edit: 05 Jan 2025 19:14 by thomas.

Please Log in or Create an account to join the conversation.

More
05 Jan 2025 19:19 #10508 by thomas

Please Log in or Create an account to join the conversation.

Time to create page: 0.174 seconds