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

Simplify string handling #1048

Draft
wants to merge 13 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions contrib/adduser.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
#include <syslog.h>

#include "string/strcmp/streq.h"
#include "string/strcmp/strprefix.h"


#define IMMEDIATE_CHANGE /* Expire newly created password, must be changed
Expand Down Expand Up @@ -224,7 +225,7 @@ main (void)
printf ("That name is in use, choose another.\n");
done = 0;
}
else if (strchr (usrname, ' ') != NULL)
else if (strchr(usrname, ' '))
{
printf ("No spaces in username!!\n");
done = 0;
Expand Down Expand Up @@ -389,7 +390,7 @@ main (void)
fflush (stdout);
safeget (foo, sizeof (foo));

done = bad = correct = (foo[0] == 'y' || foo[0] == 'Y');
done = bad = correct = (strprefix(foo, "y") || strprefix(foo, "Y"));

if (bad != 1)
printf ("\nUser [%s] not added\n", usrname);
Expand Down
2 changes: 2 additions & 0 deletions lib/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ libshadow_la_SOURCES = \
string/strchr/strrspn.h \
string/strcmp/streq.c \
string/strcmp/streq.h \
string/strcmp/strprefix.c \
string/strcmp/strprefix.h \
string/strcpy/stpecpy.c \
string/strcpy/stpecpy.h \
string/strcpy/strncat.c \
Expand Down
8 changes: 4 additions & 4 deletions lib/chkname.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ is_valid_name(const char *name)
*/
int numeric;

if ('\0' == *name ||
('.' == *name && (('.' == name[1] && '\0' == name[2]) ||
'\0' == name[1])) ||
if (streq(name, "") ||
streq(name, ".") ||
streq(name, "..") ||
!((*name >= 'a' && *name <= 'z') ||
(*name >= 'A' && *name <= 'Z') ||
(*name >= '0' && *name <= '9') ||
Expand All @@ -95,7 +95,7 @@ is_valid_name(const char *name)
*name == '_' ||
*name == '.' ||
*name == '-' ||
(*name == '$' && name[1] == '\0')
streq(name, "$")
))
{
errno = EINVAL;
Expand Down
3 changes: 2 additions & 1 deletion lib/commonio.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "string/memset/memzero.h"
#include "string/sprintf/snprintf.h"
#include "string/strcmp/streq.h"
#include "string/strcmp/strprefix.h"
#include "string/strtok/stpsep.h"


Expand Down Expand Up @@ -524,7 +525,7 @@ static void add_one_entry (struct commonio_db *db,

static bool name_is_nis (const char *name)
{
return (('+' == name[0]) || ('-' == name[0]));
return strprefix(name, "+") || strprefix(name, "-");
}


Expand Down
5 changes: 2 additions & 3 deletions lib/console.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "getdef.h"
#include "prototypes.h"
#include "string/strcmp/streq.h"
#include "string/strcmp/strprefix.h"
#include "string/strcpy/strtcpy.h"
#include "string/strtok/stpsep.h"

Expand Down Expand Up @@ -103,9 +104,7 @@ is_listed(const char *cfgin, const char *tty, bool def)

bool console (const char *tty)
{
if (strncmp (tty, "/dev/", 5) == 0) {
tty += 5;
}
tty = strprefix(tty, "/dev/") ?: tty;

return is_listed ("CONSOLE", tty, true);
}
Expand Down
3 changes: 2 additions & 1 deletion lib/copydir.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "shadowlog.h"
#include "string/sprintf/xasprintf.h"
#include "string/strcmp/streq.h"
#include "string/strcmp/strprefix.h"


static /*@null@*/const char *src_orig;
Expand Down Expand Up @@ -576,7 +577,7 @@ static int copy_symlink (const struct path_info *src, const struct path_info *ds
* create a link to the corresponding entry in the dst_orig
* directory.
*/
if (strncmp(oldlink, src_orig, strlen(src_orig)) == 0) {
if (strprefix(oldlink, src_orig)) {
char *dummy;

xasprintf(&dummy, "%s%s", dst_orig, oldlink + strlen(src_orig));
Expand Down
4 changes: 3 additions & 1 deletion lib/encrypt.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#include "prototypes.h"
#include "defines.h"
#include "shadowlog_internal.h"
#include "string/strcmp/strprefix.h"


/*@exposed@*//*@null@*/char *pw_encrypt (const char *clear, const char *salt)
{
Expand All @@ -35,7 +37,7 @@

/* Some crypt() do not return NULL if the algorithm is not
* supported, and return a DES encrypted password. */
if ((NULL != salt) && (salt[0] == '$') && (strlen (cp) <= 13))
if ((NULL != salt) && strprefix(salt, "$") && (strlen (cp) <= 13))
{
/*@observer@*/const char *method;
switch (salt[1])
Expand Down
11 changes: 6 additions & 5 deletions lib/env.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "shadowlog.h"
#include "string/sprintf/snprintf.h"
#include "string/sprintf/xasprintf.h"
#include "string/strcmp/strprefix.h"
#include "string/strdup/xstrdup.h"


Expand Down Expand Up @@ -175,7 +176,7 @@ void set_env (int argc, char *const *argv)
const char *const *p;

for (p = forbid; NULL != *p; p++) {
if (strncmp (*argv, *p, strlen (*p)) == 0) {
if (strprefix(*argv, *p)) {
break;
}
}
Expand Down Expand Up @@ -210,7 +211,7 @@ void sanitize_env (void)

for (cur = envp; NULL != *cur; cur++) {
for (bad = forbid; NULL != *bad; bad++) {
if (strncmp (*cur, *bad, strlen (*bad)) == 0) {
if (strprefix(*cur, *bad)) {
for (move = cur; NULL != *move; move++) {
*move = *(move + 1);
}
Expand All @@ -222,12 +223,12 @@ void sanitize_env (void)

for (cur = envp; NULL != *cur; cur++) {
for (bad = noslash; NULL != *bad; bad++) {
if (strncmp (*cur, *bad, strlen (*bad)) != 0) {
if (!strprefix(*cur, *bad)) {
continue;
}
if (strchr (*cur, '/') == NULL) {
if (!strchr(*cur, '/'))
continue; /* OK */
}

for (move = cur; NULL != *move; move++) {
*move = *(move + 1);
}
Expand Down
3 changes: 2 additions & 1 deletion lib/getdate.y
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "getdate.h"
#include "string/strchr/stpspn.h"
#include "string/strcmp/streq.h"
#include "string/strcmp/strprefix.h"


/* Some old versions of bison generate parsers that use bcopy.
Expand Down Expand Up @@ -657,7 +658,7 @@ static int LookupWord (char *buff)
{
if (abbrev)
{
if (strncmp (buff, tp->name, 3) == 0)
if (strprefix(tp->name, buff))
{
yylval.Number = tp->value;
return tp->type;
Expand Down
3 changes: 2 additions & 1 deletion lib/getdef.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "string/strchr/stpspn.h"
#include "string/strchr/strrspn.h"
#include "string/strcmp/streq.h"
#include "string/strcmp/strprefix.h"
#include "string/strtok/stpsep.h"


Expand Down Expand Up @@ -567,7 +568,7 @@ static void def_load (void)
* Break the line into two fields.
*/
name = stpspn(buf, " \t"); /* first nonwhite */
if (streq(name, "") || *name == '#')
if (streq(name, "") || strprefix(name, "#"))
continue; /* comment or empty */

s = stpsep(name, " \t"); /* next field */
Expand Down
30 changes: 18 additions & 12 deletions lib/limits.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "string/memset/memzero.h"
#include "string/strchr/stpspn.h"
#include "string/strcmp/streq.h"
#include "string/strcmp/strprefix.h"
#include "typetraits.h"


Expand All @@ -62,7 +63,7 @@
/* The "-" is special, not belonging to a strange negative limit.
* It is infinity, in a controlled way.
*/
if ('-' == value[0]) {
if (strprefix(value, "-")) {
limit = RLIM_INFINITY;

} else {
Expand Down Expand Up @@ -370,7 +371,7 @@
* FIXME: A better (smarter) checking should be done
*/
while (fgets (buf, 1024, fil) != NULL) {
if (('#' == buf[0]) || ('\n' == buf[0])) {
if (strprefix(buf, "#") || strprefix(buf, "\n")) {
continue;
}
MEMZERO(tempbuf);
Expand Down Expand Up @@ -401,7 +402,7 @@
break;
} else if (streq(name, "*")) {
strcpy (deflimits, tempbuf);
} else if (name[0] == '@') {
} else if (strprefix(name, "@")) {
/* If the user is in the group, the group
* limits apply unless later a line for
* the specific user is found.
Expand Down Expand Up @@ -473,14 +474,15 @@
}
}
for (cp = info->pw_gecos; cp != NULL; cp = strchr (cp, ',')) {
if (',' == *cp) {
cp++;
}
char *val;

cp = strprefix(cp, ",") ?: cp;

Check notice

Code scanning / CodeQL

For loop variable changed in body Note

Loop counters should not be modified in the body of the
loop
.

if (strncmp (cp, "pri=", 4) == 0) {
val = strprefix(cp, "pri=");
if (val != NULL) {
int inc;

if (a2si(&inc, cp + 4, NULL, 0, -20, 20) == 0) {
if (a2si(&inc, val, NULL, 0, -20, 20) == 0) {
errno = 0;
if ( (nice (inc) != -1)
|| (0 != errno)) {
Expand All @@ -495,21 +497,25 @@

continue;
}
if (strncmp (cp, "ulimit=", 7) == 0) {

val = strprefix(cp, "ulimit=");
if (val != NULL) {
int blocks;

if ( (str2si(&blocks, cp + 7) == -1)
if ( (str2si(&blocks, val) == -1)
|| (set_filesize_limit (blocks) != 0)) {
SYSLOG ((LOG_WARN,
"Can't set the ulimit for user %s",
info->pw_name));
}
continue;
}
if (strncmp (cp, "umask=", 6) == 0) {

val = strprefix(cp, "umask=");
if (val != NULL) {
mode_t mask;

if (str2i(mode_t, &mask, cp + 6) == -1) {
if (str2i(mode_t, &mask, val) == -1) {
SYSLOG ((LOG_WARN,
"Can't set umask value for user %s",
info->pw_name));
Expand Down
3 changes: 2 additions & 1 deletion lib/nss.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "string/sprintf/snprintf.h"
#include "string/strchr/stpspn.h"
#include "string/strcmp/streq.h"
#include "string/strcmp/strprefix.h"
#include "string/strtok/stpsep.h"


Expand Down Expand Up @@ -79,7 +80,7 @@ nss_init(const char *nsswitch_path) {
}
p = NULL;
while (getline(&line, &len, nssfp) != -1) {
if (line[0] == '#')
if (strprefix(line, "#"))
continue;
if (strlen(line) < 8)
continue;
Expand Down
4 changes: 2 additions & 2 deletions lib/obscure.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include <ctype.h>
#include <stdio.h>
#include <string.h>

#include "attr.h"
#include "prototypes.h"
Expand Down Expand Up @@ -66,9 +67,8 @@ static bool similar (/*@notnull@*/const char *old, /*@notnull@*/const char *new)
}

for (i = j = 0; ('\0' != new[i]) && ('\0' != old[i]); i++) {
if (strchr (new, old[i]) != NULL) {
if (strchr(new, old[i]))
j++;
}
}

if (i >= j * 2) {
Expand Down
5 changes: 3 additions & 2 deletions lib/port.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "port.h"
#include "prototypes.h"
#include "string/strcmp/streq.h"
#include "string/strcmp/strprefix.h"
#include "string/strtok/stpsep.h"


Expand Down Expand Up @@ -50,7 +51,7 @@ static int portcmp (const char *pattern, const char *port)
if (streq(orig, "SU"))
return 1;

return (*pattern == '*') ? 0 : 1;
return !strprefix(pattern, "*");
}

/*
Expand Down Expand Up @@ -140,7 +141,7 @@ getportent(void)
errno = saveerr;
return NULL;
}
if ('#' == buf[0])
if (strprefix(buf, "#"))
goto next;

stpsep(buf, "\n");
Expand Down
19 changes: 9 additions & 10 deletions lib/prefix_flag.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "shadowlog.h"
#include "string/sprintf/xasprintf.h"
#include "string/strcmp/streq.h"
#include "string/strcmp/strprefix.h"


static char *passwd_db_file = NULL;
Expand All @@ -53,17 +54,15 @@
*/
extern const char* process_prefix_flag (const char* short_opt, int argc, char **argv)
{
/*
* Parse the command line options.
*/
int i;
const char *prefix = NULL, *val;

for (i = 0; i < argc; i++) {
val = NULL;
const char *prefix = NULL;

for (int i = 0; i < argc; i++) {
const char *val;

val = strprefix(argv[i], "--prefix=");

if ( streq(argv[i], "--prefix")
|| ((strncmp (argv[i], "--prefix=", 9) == 0)
&& (val = argv[i] + 9))
|| val != NULL
|| streq(argv[i], short_opt))
{
if (NULL != prefix) {
Expand All @@ -82,7 +81,7 @@
exit (E_BAD_ARG);
} else {
prefix = argv[++ i];
}

Check notice

Code scanning / CodeQL

For loop variable changed in body Note

Loop counters should not be modified in the body of the
loop
.
}
}

Expand Down
Loading
Loading