treewide: Make a bunch of pointer variables pointers to const

Sufficiently recent cppcheck (I'm using 2.13.0) seems to have added another
warning for pointer variables which could be pointer to const but aren't.
Use this to make a bunch of variables const pointers where they previously
weren't for no particular reason.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
This commit is contained in:
David Gibson 2024-01-15 17:39:43 +11:00 committed by Stefano Brivio
parent f60c85194b
commit a179ca6707
11 changed files with 41 additions and 34 deletions

3
arch.c
View file

@ -25,7 +25,8 @@
#ifdef __x86_64__ #ifdef __x86_64__
void arch_avx2_exec(char **argv) void arch_avx2_exec(char **argv)
{ {
char exe[PATH_MAX] = { 0 }, *p; char exe[PATH_MAX] = { 0 };
const char *p;
if (readlink("/proc/self/exe", exe, PATH_MAX - 1) < 0) { if (readlink("/proc/self/exe", exe, PATH_MAX - 1) < 0) {
perror("readlink /proc/self/exe"); perror("readlink /proc/self/exe");

12
conf.c
View file

@ -412,8 +412,9 @@ static void get_dns(struct ctx *c)
int dns4_set, dns6_set, dnss_set, dns_set, fd; int dns4_set, dns6_set, dnss_set, dns_set, fd;
struct fqdn *s = c->dns_search; struct fqdn *s = c->dns_search;
struct lineread resolvconf; struct lineread resolvconf;
char *line, *end;
const char *p;
int line_len; int line_len;
char *line, *p, *end;
dns4_set = !c->ifi4 || !IN4_IS_ADDR_UNSPECIFIED(dns4); dns4_set = !c->ifi4 || !IN4_IS_ADDR_UNSPECIFIED(dns4);
dns6_set = !c->ifi6 || !IN6_IS_ADDR_UNSPECIFIED(dns6); dns6_set = !c->ifi6 || !IN6_IS_ADDR_UNSPECIFIED(dns6);
@ -1025,7 +1026,7 @@ static int conf_runas(char *opt, unsigned int *uid, unsigned int *gid)
if (*endptr) { if (*endptr) {
#ifndef GLIBC_NO_STATIC_NSS #ifndef GLIBC_NO_STATIC_NSS
/* Not numeric, look up as a username */ /* Not numeric, look up as a username */
struct passwd *pw; const struct passwd *pw;
/* cppcheck-suppress getpwnamCalled */ /* cppcheck-suppress getpwnamCalled */
if (!(pw = getpwnam(uopt)) || !(*uid = pw->pw_uid)) if (!(pw = getpwnam(uopt)) || !(*uid = pw->pw_uid))
return -ENOENT; return -ENOENT;
@ -1042,7 +1043,7 @@ static int conf_runas(char *opt, unsigned int *uid, unsigned int *gid)
if (*endptr) { if (*endptr) {
#ifndef GLIBC_NO_STATIC_NSS #ifndef GLIBC_NO_STATIC_NSS
/* Not numeric, look up as a group name */ /* Not numeric, look up as a group name */
struct group *gr; const struct group *gr;
/* cppcheck-suppress getgrnamCalled */ /* cppcheck-suppress getgrnamCalled */
if (!(gr = getgrnam(gopt))) if (!(gr = getgrnam(gopt)))
return -ENOENT; return -ENOENT;
@ -1086,7 +1087,7 @@ static void conf_ugid(char *runas, uid_t *uid, gid_t *gid)
warn("Don't run as root. Changing to nobody..."); warn("Don't run as root. Changing to nobody...");
{ {
#ifndef GLIBC_NO_STATIC_NSS #ifndef GLIBC_NO_STATIC_NSS
struct passwd *pw; const struct passwd *pw;
/* cppcheck-suppress getpwnamCalled */ /* cppcheck-suppress getpwnamCalled */
pw = getpwnam("nobody"); pw = getpwnam("nobody");
if (!pw) { if (!pw) {
@ -1173,14 +1174,15 @@ void conf(struct ctx *c, int argc, char **argv)
bool copy_addrs_opt = false, copy_routes_opt = false; bool copy_addrs_opt = false, copy_routes_opt = false;
enum port_fwd_mode fwd_default = FWD_NONE; enum port_fwd_mode fwd_default = FWD_NONE;
bool v4_only = false, v6_only = false; bool v4_only = false, v6_only = false;
char *runas = NULL, *logfile = NULL;
struct in6_addr *dns6 = c->ip6.dns; struct in6_addr *dns6 = c->ip6.dns;
struct fqdn *dnss = c->dns_search; struct fqdn *dnss = c->dns_search;
struct in_addr *dns4 = c->ip4.dns; struct in_addr *dns4 = c->ip4.dns;
unsigned int ifi4 = 0, ifi6 = 0; unsigned int ifi4 = 0, ifi6 = 0;
const char *logfile = NULL;
const char *optstring; const char *optstring;
int name, ret, b, i; int name, ret, b, i;
size_t logsize = 0; size_t logsize = 0;
char *runas = NULL;
uid_t uid; uid_t uid;
gid_t gid; gid_t gid;

9
dhcp.c
View file

@ -275,10 +275,10 @@ static void opt_set_dns_search(const struct ctx *c, size_t max_len)
int dhcp(const struct ctx *c, const struct pool *p) int dhcp(const struct ctx *c, const struct pool *p)
{ {
size_t mlen, len, offset = 0, opt_len, opt_off = 0; size_t mlen, len, offset = 0, opt_len, opt_off = 0;
const struct ethhdr *eh;
const struct iphdr *iph;
const struct udphdr *uh;
struct in_addr mask; struct in_addr mask;
struct ethhdr *eh;
struct iphdr *iph;
struct udphdr *uh;
unsigned int i; unsigned int i;
struct msg *m; struct msg *m;
@ -312,7 +312,8 @@ int dhcp(const struct ctx *c, const struct pool *p)
offset += offsetof(struct msg, o); offset += offsetof(struct msg, o);
while (opt_off + 2 < opt_len) { while (opt_off + 2 < opt_len) {
uint8_t *olen, *type, *val; const uint8_t *olen, *val;
uint8_t *type;
type = packet_get(p, 0, offset + opt_off, 1, NULL); type = packet_get(p, 0, offset + opt_off, 1, NULL);
olen = packet_get(p, 0, offset + opt_off + 1, 1, NULL); olen = packet_get(p, 0, offset + opt_off + 1, 1, NULL);

View file

@ -426,10 +426,11 @@ search:
int dhcpv6(struct ctx *c, const struct pool *p, int dhcpv6(struct ctx *c, const struct pool *p,
const struct in6_addr *saddr, const struct in6_addr *daddr) const struct in6_addr *saddr, const struct in6_addr *daddr)
{ {
struct opt_hdr *ia, *bad_ia, *client_id, *server_id; struct opt_hdr *ia, *bad_ia, *client_id;
struct in6_addr *src; const struct opt_hdr *server_id;
struct msg_hdr *mh; const struct in6_addr *src;
struct udphdr *uh; const struct msg_hdr *mh;
const struct udphdr *uh;
size_t mlen, n; size_t mlen, n;
uh = packet_get(p, 0, 0, sizeof(*uh), &mlen); uh = packet_get(p, 0, 0, sizeof(*uh), &mlen);

6
log.c
View file

@ -222,7 +222,8 @@ void logfile_init(const char *name, const char *path, size_t size)
*/ */
static void logfile_rotate_fallocate(int fd, const struct timespec *ts) static void logfile_rotate_fallocate(int fd, const struct timespec *ts)
{ {
char buf[BUFSIZ], *nl; char buf[BUFSIZ];
const char *nl;
int n; int n;
if (lseek(fd, 0, SEEK_SET) == -1) if (lseek(fd, 0, SEEK_SET) == -1)
@ -260,7 +261,8 @@ static void logfile_rotate_fallocate(int fd, const struct timespec *ts)
static void logfile_rotate_move(int fd, const struct timespec *ts) static void logfile_rotate_move(int fd, const struct timespec *ts)
{ {
int header_len, write_offset, end, discard, n; int header_len, write_offset, end, discard, n;
char buf[BUFSIZ], *nl; char buf[BUFSIZ];
const char *nl;
header_len = snprintf(buf, BUFSIZ, header_len = snprintf(buf, BUFSIZ,
"%s - log truncated at %lli.%04lli\n", log_header, "%s - log truncated at %lli.%04lli\n", log_header,

4
qrap.c
View file

@ -251,8 +251,8 @@ int main(int argc, char **argv)
} }
if (!strcmp(argv[i], "-device") && i + 1 < argc) { if (!strcmp(argv[i], "-device") && i + 1 < argc) {
char *template = NULL; const char *template = NULL;
char *p; const char *p;
has_dev = 1; has_dev = 1;

12
tap.c
View file

@ -600,10 +600,10 @@ static int tap4_handler(struct ctx *c, const struct pool *in,
resume: resume:
for (seq_count = 0, seq = NULL; i < in->count; i++) { for (seq_count = 0, seq = NULL; i < in->count; i++) {
size_t l2_len, l3_len, hlen, l4_len; size_t l2_len, l3_len, hlen, l4_len;
struct ethhdr *eh; const struct ethhdr *eh;
const struct udphdr *uh;
struct iphdr *iph; struct iphdr *iph;
struct udphdr *uh; const char *l4h;
char *l4h;
packet_get(in, i, 0, 0, &l2_len); packet_get(in, i, 0, 0, &l2_len);
@ -765,9 +765,9 @@ resume:
for (seq_count = 0, seq = NULL; i < in->count; i++) { for (seq_count = 0, seq = NULL; i < in->count; i++) {
size_t l4_len, plen, check; size_t l4_len, plen, check;
struct in6_addr *saddr, *daddr; struct in6_addr *saddr, *daddr;
const struct ethhdr *eh;
const struct udphdr *uh;
struct ipv6hdr *ip6h; struct ipv6hdr *ip6h;
struct ethhdr *eh;
struct udphdr *uh;
uint8_t proto; uint8_t proto;
char *l4h; char *l4h;
@ -936,7 +936,7 @@ static void tap_sock_reset(struct ctx *c)
void tap_handler_passt(struct ctx *c, uint32_t events, void tap_handler_passt(struct ctx *c, uint32_t events,
const struct timespec *now) const struct timespec *now)
{ {
struct ethhdr *eh; const struct ethhdr *eh;
ssize_t n, rem; ssize_t n, rem;
char *p; char *p;

8
tcp.c
View file

@ -2296,7 +2296,7 @@ static int tcp_data_from_tap(struct ctx *c, struct tcp_tap_conn *conn,
for (i = idx, iov_i = 0; i < (int)p->count; i++) { for (i = idx, iov_i = 0; i < (int)p->count; i++) {
uint32_t seq, seq_offset, ack_seq; uint32_t seq, seq_offset, ack_seq;
struct tcphdr *th; const struct tcphdr *th;
char *data; char *data;
size_t off; size_t off;
@ -2517,10 +2517,10 @@ int tcp_tap_handler(struct ctx *c, uint8_t pif, int af,
const struct pool *p, int idx, const struct timespec *now) const struct pool *p, int idx, const struct timespec *now)
{ {
struct tcp_tap_conn *conn; struct tcp_tap_conn *conn;
const struct tcphdr *th;
size_t optlen, len; size_t optlen, len;
struct tcphdr *th; const char *opts;
int ack_due = 0; int ack_due = 0;
char *opts;
int count; int count;
(void)pif; (void)pif;
@ -3038,7 +3038,7 @@ void tcp_ns_sock_init(const struct ctx *c, in_port_t port)
*/ */
static int tcp_ns_socks_init(void *arg) static int tcp_ns_socks_init(void *arg)
{ {
struct ctx *c = (struct ctx *)arg; const struct ctx *c = (const struct ctx *)arg;
unsigned port; unsigned port;
ns_enter(c); ns_enter(c);

View file

@ -127,7 +127,7 @@ static int tcp_splice_epoll_ctl(const struct ctx *c,
struct tcp_splice_conn *conn) struct tcp_splice_conn *conn)
{ {
int m = conn->in_epoll ? EPOLL_CTL_MOD : EPOLL_CTL_ADD; int m = conn->in_epoll ? EPOLL_CTL_MOD : EPOLL_CTL_ADD;
union epoll_ref ref[SIDES] = { const union epoll_ref ref[SIDES] = {
{ .type = EPOLL_TYPE_TCP, .fd = conn->s[0], { .type = EPOLL_TYPE_TCP, .fd = conn->s[0],
.flowside = FLOW_SIDX(conn, 0) }, .flowside = FLOW_SIDX(conn, 0) },
{ .type = EPOLL_TYPE_TCP, .fd = conn->s[1], { .type = EPOLL_TYPE_TCP, .fd = conn->s[1],

6
udp.c
View file

@ -821,10 +821,10 @@ int udp_tap_handler(struct ctx *c, uint8_t pif,
struct iovec m[UIO_MAXIOV]; struct iovec m[UIO_MAXIOV];
struct sockaddr_in6 s_in6; struct sockaddr_in6 s_in6;
struct sockaddr_in s_in; struct sockaddr_in s_in;
const struct udphdr *uh;
struct sockaddr *sa; struct sockaddr *sa;
int i, s, count = 0; int i, s, count = 0;
in_port_t src, dst; in_port_t src, dst;
struct udphdr *uh;
socklen_t sl; socklen_t sl;
(void)c; (void)c;
@ -1045,7 +1045,7 @@ int udp_sock_init(const struct ctx *c, int ns, sa_family_t af,
* udp_sock_init_init() - Bind sockets in init namespace for inbound connections * udp_sock_init_init() - Bind sockets in init namespace for inbound connections
* @c: Execution context * @c: Execution context
*/ */
static void udp_sock_init_init(struct ctx *c) static void udp_sock_init_init(const struct ctx *c)
{ {
unsigned dst; unsigned dst;
@ -1065,7 +1065,7 @@ static void udp_sock_init_init(struct ctx *c)
*/ */
int udp_sock_init_ns(void *arg) int udp_sock_init_ns(void *arg)
{ {
struct ctx *c = (struct ctx *)arg; const struct ctx *c = (const struct ctx *)arg;
unsigned dst; unsigned dst;
ns_enter(c); ns_enter(c);

4
util.c
View file

@ -48,8 +48,8 @@
char *ipv6_l4hdr(const struct pool *p, int idx, size_t offset, uint8_t *proto, char *ipv6_l4hdr(const struct pool *p, int idx, size_t offset, uint8_t *proto,
size_t *dlen) size_t *dlen)
{ {
struct ipv6_opt_hdr *o; const struct ipv6_opt_hdr *o;
struct ipv6hdr *ip6h; const struct ipv6hdr *ip6h;
char *base; char *base;
int hdrlen; int hdrlen;
uint8_t nh; uint8_t nh;