Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

halt: switch __progname tests to use strncmp #70

Merged
merged 1 commit into from Mar 10, 2021

Conversation

heliocat
Copy link

@heliocat heliocat commented Mar 9, 2021

The pending change to allow for alternate init power controls requires
that the halt binary provided here be somewhat less strict in its
argv[0] matching in order to allow for halt, reboot, and poweroff to
still function in situations where the binary has been renamed.

This switches from strcmp(__progname, "string") to strncmp(__progname,
"string", len), where len is the name of the power command sans any
suffix.

Signed-off-by: Colin Booth colin@heliocat.net

The pending change to allow for alternate init power controls requires
that the halt binary provided here be somewhat less strict in its
argv[0] matching in order to allow for halt, reboot, and poweroff to
still function in situations where the binary has been renamed.

This switches from strcmp(__progname, "string") to strncmp(__progname,
"string", len), where len is the name of the power command sans any
suffix.

Signed-off-by: Colin Booth <colin@heliocat.net>
@heliocat
Copy link
Author

heliocat commented Mar 9, 2021

@Duncaen @leahneukirchen

Copy link
Member

@pullmoll pullmoll left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a personal preference because I refrain from using numeric constants for string lengths. This has been proven to be error prone in copy+pasta cases.

static const char s_halt[] = "halt";
static const char s_reboot[] = "reboot";
static const char s_poweroff[] = "poweroff";

…

if (strncmp(__progname, s_halt, sizeof(s_halt)-1) == 0)
    action = HALT;
if (strncmp(__progname, s_reboot, sizeof(s_reboot)-1) == 0)
    action = REBOOT;
if (strncmp(__progname, s_poweroff, sizeof(s_poweroff)-1) == 0)
    action = POWEROFF;

The else if is IMO redundant, i.e. last hit wins...

Just my .02€

@heliocat
Copy link
Author

heliocat commented Mar 9, 2021

Just a personal preference because I refrain from using numeric constants for string lengths. This has been proven to be error prone in copy+pasta cases.

static const char s_halt[] = "halt";
static const char s_reboot[] = "reboot";
static const char s_poweroff[] = "poweroff";

…

if (strncmp(__progname, s_halt, sizeof(s_halt)-1) == 0)
    action = HALT;
if (strncmp(__progname, s_reboot, sizeof(s_reboot)-1) == 0)
    action = REBOOT;
if (strncmp(__progname, s_poweroff, sizeof(s_poweroff)-1) == 0)
    action = POWEROFF;

The else if is IMO redundant, i.e. last hit wins...

Just my .02€

Thanks. I'll keep that in mind if we ever give this a more heavy refactor but I wanted to keep the diff as minimal as possible.

@leahneukirchen leahneukirchen merged commit d5fd7d1 into void-linux:master Mar 10, 2021
@heliocat heliocat deleted the fuzzy_match_halt branch March 11, 2021 07:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants