Skip to content

Clean up use of strcmp/strncmp/strncasecmp for command line arguments #104

Closed
AlexanderAmelkin opened this issue Feb 27, 2019 · 1 comment
Assignees
Labels
cleanup The issue is to clean up or refactor the existing code

Comments

@AlexanderAmelkin
Copy link
Contributor

AlexanderAmelkin commented Feb 27, 2019

This is associated with SourceForge ticket 24.
In many places command line arguments are matched using some weird constructions like

if (strncmp(argv[i], "option", strlen("option")) == 0)

or

if (strncmp(argv[i], "option", 7) == 0)

or

if (strncmp(argv[i], "option", 7) == 0 && strncmp(argv[i], "option", strlen(argv[i])) == 0)

or even

if (! (strncmp(argv[i], "option", __maxlen(7, strlen(argv[i])) == 0) )

In some places even strncasecmp() is used.

All those approaches look awfully redundant, cumbersome and unprofessional. They also lead to problems described in aforementioned ticket.

This issue is to capture the effort to clean them all up and switch to using simple strcmp() wherever normal C strings coming from command line arguments or console input are compared.

There are also places where strncmp() is used to compare IPMI strings without the null-byte terminator and with a type/length byte prefix instead. Those must be left as is (or maybe converted to using some new helper function like ipmi_strcmp()).

doublechiang added a commit to doublechiang/ipmitool that referenced this issue Feb 21, 2020
@doublechiang
Copy link
Contributor

anything I can do for this ticket?

doublechiang added a commit to doublechiang/ipmitool that referenced this issue May 23, 2020
doublechiang added a commit to doublechiang/ipmitool that referenced this issue May 26, 2020
doublechiang pushed a commit to doublechiang/ipmitool that referenced this issue Apr 24, 2022
Unify the comparison idioms use.
Always use `if(!strcmp())` for "if string equals"
and `if(strcmp())` for "if string is not equal".
Never use `== 0` and `!= 0` with `strcmp()`.

Minor reformatting of the code immediately surrounding the
refactored lines.

Resolves ipmitool#104

Signed-off-by: Alexander Amelkin <alexander@amelkin.msk.ru>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
cleanup The issue is to clean up or refactor the existing code
Projects
None yet
Development

No branches or pull requests

2 participants