Introduce str_strip_prefix_len function and STR_STRIP_PREFIX macro
Address proliferation of string prefix check implementations. * defs.h (str_strip_prefix_len): New inline function. (STR_STRIP_PREFIX): New macro. * qualify.c (strip_prefix): Remove. (parse_inject_token): Use STR_STRIP_PREFIX instead of strip_prefix. (qualify): Use str_strip_prefix_len. * socketutils.c (netlink_parse_response): Likewise. * util.c (printfd): Likewise.
This commit is contained in:
parent
28e478a6eb
commit
1459d07f94
14
defs.h
14
defs.h
@ -511,6 +511,20 @@ string_to_uint_upto(const char *const str, unsigned int max_val)
|
||||
}
|
||||
extern int next_set_bit(const void *bit_array, unsigned cur_bit, unsigned size_bits);
|
||||
|
||||
/*
|
||||
* Returns STR if it does not start with PREFIX,
|
||||
* or a pointer to the first char in STR after PREFIX.
|
||||
* The length of PREFIX is specified by PREFIX_LEN.
|
||||
*/
|
||||
static inline const char *
|
||||
str_strip_prefix_len(const char *str, const char *prefix, size_t prefix_len)
|
||||
{
|
||||
return strncmp(str, prefix, prefix_len) ? str : str + prefix_len;
|
||||
}
|
||||
|
||||
#define STR_STRIP_PREFIX(str, prefix) \
|
||||
str_strip_prefix_len((str), (prefix), sizeof(prefix) - 1)
|
||||
|
||||
#define QUOTE_0_TERMINATED 0x01
|
||||
#define QUOTE_OMIT_LEADING_TRAILING_QUOTES 0x02
|
||||
#define QUOTE_OMIT_TRAILING_0 0x08
|
||||
|
32
qualify.c
32
qualify.c
@ -414,18 +414,6 @@ handle_inversion:
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Returns NULL if STR does not start with PREFIX,
|
||||
* or a pointer to the first char in STR after PREFIX.
|
||||
*/
|
||||
static const char *
|
||||
strip_prefix(const char *prefix, const char *str)
|
||||
{
|
||||
size_t len = strlen(prefix);
|
||||
|
||||
return strncmp(prefix, str, len) ? NULL : str + len;
|
||||
}
|
||||
|
||||
static int
|
||||
find_errno_by_name(const char *name)
|
||||
{
|
||||
@ -446,7 +434,7 @@ parse_inject_token(const char *const token, struct inject_opts *const fopts,
|
||||
const char *val;
|
||||
int intval;
|
||||
|
||||
if ((val = strip_prefix("when=", token))) {
|
||||
if ((val = STR_STRIP_PREFIX(token, "when=")) != token) {
|
||||
/*
|
||||
* == 1+1
|
||||
* F == F+0
|
||||
@ -476,7 +464,7 @@ parse_inject_token(const char *const token, struct inject_opts *const fopts,
|
||||
/* F == F+0 */
|
||||
fopts->step = 0;
|
||||
}
|
||||
} else if ((val = strip_prefix("error=", token))) {
|
||||
} else if ((val = STR_STRIP_PREFIX(token, "error=")) != token) {
|
||||
if (fopts->rval != INJECT_OPTS_RVAL_DEFAULT)
|
||||
return false;
|
||||
intval = string_to_uint_upto(val, MAX_ERRNO_VALUE);
|
||||
@ -485,14 +473,16 @@ parse_inject_token(const char *const token, struct inject_opts *const fopts,
|
||||
if (intval < 1)
|
||||
return false;
|
||||
fopts->rval = -intval;
|
||||
} else if (!fault_tokens_only && (val = strip_prefix("retval=", token))) {
|
||||
} else if (!fault_tokens_only
|
||||
&& (val = STR_STRIP_PREFIX(token, "retval=")) != token) {
|
||||
if (fopts->rval != INJECT_OPTS_RVAL_DEFAULT)
|
||||
return false;
|
||||
intval = string_to_uint(val);
|
||||
if (intval < 0)
|
||||
return false;
|
||||
fopts->rval = intval;
|
||||
} else if (!fault_tokens_only && (val = strip_prefix("signal=", token))) {
|
||||
} else if (!fault_tokens_only
|
||||
&& (val = STR_STRIP_PREFIX(token, "signal=")) != token) {
|
||||
intval = sigstr_to_uint(val);
|
||||
if (intval < 1 || intval > NSIG_BYTES * 8)
|
||||
return false;
|
||||
@ -677,14 +667,14 @@ qualify(const char *str)
|
||||
unsigned int i;
|
||||
|
||||
for (i = 0; i < ARRAY_SIZE(qual_options); ++i) {
|
||||
const char *p = qual_options[i].name;
|
||||
unsigned int len = strlen(p);
|
||||
const char *name = qual_options[i].name;
|
||||
const size_t len = strlen(name);
|
||||
const char *val = str_strip_prefix_len(str, name, len);
|
||||
|
||||
if (strncmp(str, p, len) || str[len] != '=')
|
||||
if (val == str || *val != '=')
|
||||
continue;
|
||||
|
||||
str = val + 1;
|
||||
opt = &qual_options[i];
|
||||
str += len + 1;
|
||||
break;
|
||||
}
|
||||
|
||||
|
@ -381,12 +381,7 @@ netlink_parse_response(const char *proto_name, const void *data,
|
||||
diag_msg->ndiag_protocol);
|
||||
|
||||
if (netlink_proto) {
|
||||
static const char netlink_prefix[] = "NETLINK_";
|
||||
const size_t netlink_prefix_len =
|
||||
sizeof(netlink_prefix) -1;
|
||||
if (strncmp(netlink_proto, netlink_prefix,
|
||||
netlink_prefix_len) == 0)
|
||||
netlink_proto += netlink_prefix_len;
|
||||
netlink_proto = STR_STRIP_PREFIX(netlink_proto, "NETLINK_");
|
||||
if (asprintf(&details, "%s:[%s:%u]", proto_name,
|
||||
netlink_proto, diag_msg->ndiag_portid) < 0)
|
||||
return -1;
|
||||
|
18
util.c
18
util.c
@ -635,17 +635,15 @@ printfd(struct tcb *tcp, int fd)
|
||||
{
|
||||
char path[PATH_MAX + 1];
|
||||
if (show_fd_path && getfdpath(tcp, fd, path, sizeof(path)) >= 0) {
|
||||
static const char socket_prefix[] = "socket:[";
|
||||
const size_t socket_prefix_len = sizeof(socket_prefix) - 1;
|
||||
const size_t path_len = strlen(path);
|
||||
const char *str;
|
||||
size_t len;
|
||||
unsigned long inode;
|
||||
|
||||
tprintf("%d<", fd);
|
||||
if (show_fd_path > 1 &&
|
||||
strncmp(path, socket_prefix, socket_prefix_len) == 0 &&
|
||||
path[path_len - 1] == ']') {
|
||||
unsigned long inode =
|
||||
strtoul(path + socket_prefix_len, NULL, 10);
|
||||
|
||||
if (show_fd_path > 1
|
||||
&& (str = STR_STRIP_PREFIX(path, "socket:[")) != path
|
||||
&& (len = strlen(str)) && str[len - 1] == ']'
|
||||
&& (inode = strtoul(str, NULL, 10))) {
|
||||
if (!print_sockaddr_by_inode_cached(inode)) {
|
||||
const enum sock_proto proto =
|
||||
getfdproto(tcp, fd);
|
||||
@ -653,7 +651,7 @@ printfd(struct tcb *tcp, int fd)
|
||||
tprints(path);
|
||||
}
|
||||
} else {
|
||||
print_quoted_string(path, path_len,
|
||||
print_quoted_string(path, strlen(path),
|
||||
QUOTE_OMIT_LEADING_TRAILING_QUOTES);
|
||||
}
|
||||
tprints(">");
|
||||
|
Loading…
x
Reference in New Issue
Block a user