Skip to content

Commit 5e84101

Browse files
committed
Fix cppcheck errors
Declare a bunch of pointers const that cppcheck figured out were never written to. Add some more cppcheck exclusions for constructs that it can't understand, and move a variable initialization to make cppcheck happy.
1 parent 8b1aced commit 5e84101

File tree

12 files changed

+67
-43
lines changed

12 files changed

+67
-43
lines changed

portable/getnameinfo.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* which can be found at <https://www.eyrie.org/~eagle/software/rra-c-util/>.
1818
*
1919
* Written by Russ Allbery <[email protected]>
20-
* Copyright 2005, 2020 Russ Allbery <[email protected]>
20+
* Copyright 2005, 2020, 2024 Russ Allbery <[email protected]>
2121
* Copyright 2008, 2011, 2013-2014
2222
* The Board of Trustees of the Leland Stanford Junior University
2323
*
@@ -108,10 +108,10 @@ static int
108108
lookup_name(const struct in_addr *addr, char *node, socklen_t nodelen,
109109
int flags)
110110
{
111-
struct hostent *host;
111+
const struct hostent *host;
112112
char **alias;
113113
int status;
114-
char *name;
114+
const char *name;
115115
size_t namelen;
116116

117117
/* Do the name lookup first unless told not to. */
@@ -155,7 +155,7 @@ static int
155155
lookup_service(unsigned short port, char *service, socklen_t servicelen,
156156
int flags)
157157
{
158-
struct servent *srv;
158+
const struct servent *srv;
159159
const char *protocol;
160160
int status;
161161
size_t namelen;

tests/data/cppcheck.supp

+16-5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
//
1818
// SPDX-License-Identifier: FSFAP
1919

20+
// I'm not going to add const to every callback function and then cast the
21+
// function type when passing in the callback.
22+
constParameterCallback
23+
2024
// I like declaring variables at the top of a function rather than cluttering
2125
// every if and loop body with declarations.
2226
variableScope
@@ -25,6 +29,13 @@ variableScope
2529
// string length.
2630
constArgument:tests/runtests.c:804
2731

32+
// cppcheck doesn't understand some cases where prototypes have to match
33+
// or where const is added via macros such as PAM_CONST.
34+
constParameterPointer:pam-util/options.c:469
35+
constParameterPointer:tests/fakepam/data.c:150
36+
constParameterPointer:tests/fakepam/general.c:131
37+
constParameterPointer:tests/fakepam/logging.c:100
38+
2839
// False positive due to recursive function.
2940
knownConditionTrueFalse:portable/getopt.c:146
3041

@@ -59,6 +70,11 @@ nullPointerRedundantCheck:tests/plugin/mit-t.c:97
5970
// Setting the variable to NULL explicitly after deallocation.
6071
redundantAssignment:tests/pam-util/options-t.c
6172

73+
// Bug in cppcheck 2.7. Two possible struct layouts are chosen based on the
74+
// ABI, and the one chosen is used, but cppcheck always warns about both even
75+
// if the preprocessor conditionals are identical.
76+
unusedStructMember:kafs/sys-solaris.c
77+
6278
// (remctl) Bug in cppcheck 1.89 (fixed in 2.3). The address of these
6379
// variables are passed to a PHP function.
6480
uninitvar:php/php_remctl.c:119
@@ -75,8 +91,3 @@ redundantAssignment:tests/server/acl-t.c
7591
// (pam-krb5) cppcheck doesn't recognize the unused attribute on labels.
7692
unusedLabel:module/auth.c:895
7793
unusedLabelConfiguration:module/auth.c:895
78-
79-
// Bug in cppcheck 2.7. Two possible struct layouts are chosen based on the
80-
// ABI, and the one chosen is used, but cppcheck always warns about both even
81-
// if the preprocessor conditionals are identical.
82-
unusedStructMember:kafs/sys-solaris.c

tests/fakepam/config.c

+17-10
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* which can be found at <https://www.eyrie.org/~eagle/software/rra-c-util/>.
1111
*
1212
* Written by Russ Allbery <[email protected]>
13-
* Copyright 2017-2018, 2020 Russ Allbery <[email protected]>
13+
* Copyright 2017-2018, 2020, 2024 Russ Allbery <[email protected]>
1414
* Copyright 2011-2012, 2014
1515
* The Board of Trustees of the Leland Stanford Junior University
1616
*
@@ -157,7 +157,8 @@ static char *
157157
readline(FILE *file)
158158
{
159159
char buffer[BUFSIZ];
160-
char *line, *first;
160+
char *line;
161+
const char *first;
161162

162163
do {
163164
line = fgets(buffer, sizeof(buffer), file);
@@ -426,7 +427,7 @@ static void
426427
split_options(char *string, struct options *options,
427428
const struct script_config *config)
428429
{
429-
char *opt;
430+
const char *opt;
430431
size_t size, count;
431432

432433
for (opt = strtok(string, " "); opt != NULL; opt = strtok(NULL, " ")) {
@@ -462,7 +463,8 @@ static void
462463
parse_options(FILE *script, struct work *work,
463464
const struct script_config *config)
464465
{
465-
char *line, *group, *token;
466+
char *line, *token;
467+
const char *group;
466468
size_t length = 0;
467469
enum group_type type;
468470

@@ -497,9 +499,10 @@ parse_options(FILE *script, struct work *work,
497499
* action struct to fill in with the call and the option flags.
498500
*/
499501
static void
500-
parse_call(char *token, struct action *action)
502+
parse_call(const char *token, struct action *action)
501503
{
502-
char *flags, *flag;
504+
char *flags;
505+
const char *flag;
503506

504507
action->flags = 0;
505508
flags = strchr(token, '(');
@@ -528,7 +531,8 @@ static struct action *
528531
parse_run(FILE *script)
529532
{
530533
struct action *head = NULL, *current = NULL, *next;
531-
char *line, *token, *call;
534+
char *line, *token;
535+
const char *call;
532536
size_t length = 0;
533537

534538
for (line = readline(script); line != NULL; line = readline(script)) {
@@ -576,7 +580,8 @@ parse_run(FILE *script)
576580
static int
577581
parse_end(FILE *script)
578582
{
579-
char *line, *token, *flag;
583+
char *line, *token;
584+
const char *flag;
580585
size_t length = 0;
581586
int flags = PAM_SUCCESS;
582587

@@ -622,7 +627,8 @@ parse_end(FILE *script)
622627
static struct output *
623628
parse_output(FILE *script, const struct script_config *config)
624629
{
625-
char *line, *token, *message;
630+
char *line, *message;
631+
const char *token;
626632
struct output *output;
627633
int priority;
628634

@@ -662,7 +668,8 @@ parse_prompts(FILE *script, const struct script_config *config)
662668
{
663669
struct prompts *prompts = NULL;
664670
struct prompt *prompt;
665-
char *line, *token, *style, *end;
671+
char *line, *token, *end;
672+
const char *style;
666673
size_t size, count, i;
667674
size_t length = 0;
668675

tests/fakepam/data.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* which can be found at <https://www.eyrie.org/~eagle/software/rra-c-util/>.
1010
*
1111
* Written by Russ Allbery <[email protected]>
12-
* Copyright 2017, 2020 Russ Allbery <[email protected]>
12+
* Copyright 2017, 2020, 2024 Russ Allbery <[email protected]>
1313
* Copyright 2010-2011, 2014
1414
* The Board of Trustees of the Leland Stanford Junior University
1515
*
@@ -51,7 +51,7 @@
5151
int
5252
pam_get_data(const pam_handle_t *pamh, const char *name, const void **data)
5353
{
54-
struct fakepam_data *item;
54+
const struct fakepam_data *item;
5555

5656
for (item = pamh->data; item != NULL; item = item->next)
5757
if (strcmp(item->name, name) == 0) {

tests/portable/getaddrinfo-t.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ main(void)
4949
struct addrinfo *ai, *first;
5050
struct addrinfo hints;
5151
struct sockaddr_in *saddr;
52-
struct hostent *host;
52+
const struct hostent *host;
5353
struct in_addr addr, *addrs;
54-
struct servent *service;
54+
const struct servent *service;
5555
int i, result, count;
5656
int found;
5757

tests/portable/getnameinfo-t.c

+4-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* which can be found at <https://www.eyrie.org/~eagle/software/rra-c-util/>.
66
*
77
* Written by Russ Allbery <[email protected]>
8-
* Copyright 2005-2006, 2014, 2018, 2022-2023 Russ Allbery <[email protected]>
8+
* Copyright 2005-2006, 2014, 2018, 2022-2024 Russ Allbery <[email protected]>
99
* Copyright 2007-2011
1010
* The Board of Trustees of the Leland Stanford Junior University
1111
*
@@ -37,10 +37,10 @@ main(void)
3737
{
3838
char node[256], service[256];
3939
struct sockaddr_in sin;
40-
struct sockaddr *sa = (struct sockaddr *) &sin;
40+
const struct sockaddr *sa;
4141
int status;
4242
struct hostent *hp;
43-
struct servent *serv;
43+
const struct servent *serv;
4444
char *name;
4545

4646
plan(26);
@@ -52,6 +52,7 @@ main(void)
5252
* the same port, but with different protocols.
5353
*/
5454
memset(&sin, 0, sizeof(sin));
55+
sa = (struct sockaddr *) &sin;
5556
sin.sin_family = AF_INET;
5657
sin.sin_port = htons(119);
5758
inet_aton("10.20.30.40", &sin.sin_addr);

tests/runtests.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
* should be sent to the e-mail address below. This program is part of C TAP
99
* Harness <https://www.eyrie.org/~eagle/software/c-tap-harness/>.
1010
*
11-
* Copyright 2000-2001, 2004, 2006-2019, 2022 Russ Allbery <[email protected]>
11+
* Copyright 2000-2001, 2004, 2006-2019, 2022, 2024
12+
* Russ Allbery <[email protected]>
1213
*
1314
* Permission is hereby granted, free of charge, to any person obtaining a
1415
* copy of this software and associated documentation files (the "Software"),
@@ -994,7 +995,7 @@ test_print_range(unsigned long first, unsigned long last, unsigned long chars,
994995
* that terminated it if it was killed by a signal.
995996
*/
996997
static void
997-
test_summarize(struct testset *ts, int status)
998+
test_summarize(const struct testset *ts, int status)
998999
{
9991000
unsigned long i;
10001001
unsigned long missing = 0;

tests/tap/basic.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* documentation is at <https://www.eyrie.org/~eagle/software/c-tap-harness/>.
1414
*
1515
* Written by Russ Allbery <[email protected]>
16-
* Copyright 2009-2019, 2021 Russ Allbery <[email protected]>
16+
* Copyright 2009-2019, 2021, 2024 Russ Allbery <[email protected]>
1717
* Copyright 2001-2002, 2004-2008, 2011-2014
1818
* The Board of Trustees of the Leland Stanford Junior University
1919
*
@@ -920,7 +920,7 @@ bstrndup(const char *s, size_t n)
920920
char *
921921
test_file_path(const char *file)
922922
{
923-
char *base;
923+
const char *base;
924924
char *path = NULL;
925925
const char *envs[] = {"C_TAP_BUILD", "C_TAP_SOURCE", NULL};
926926
int i;

tests/tap/remctl.c

+6-6
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* which can be found at <https://www.eyrie.org/~eagle/software/rra-c-util/>.
99
*
1010
* Written by Russ Allbery <[email protected]>
11-
* Copyright 2018, 2020 Russ Allbery <[email protected]>
11+
* Copyright 2018, 2020, 2024 Russ Allbery <[email protected]>
1212
* Copyright 2006-2007, 2009, 2011-2014
1313
* The Board of Trustees of the Leland Stanford Junior University
1414
*
@@ -63,8 +63,8 @@
6363
* not set, remctld_start_internal calls skip_all.
6464
*/
6565
static struct process *
66-
remctld_start_internal(struct kerberos_config *krbconf, const char *config,
67-
va_list args, bool fakeroot)
66+
remctld_start_internal(const struct kerberos_config *krbconf,
67+
const char *config, va_list args, bool fakeroot)
6868
{
6969
va_list args_copy;
7070
char *tmpdir, *pidfile, *confpath;
@@ -141,7 +141,7 @@ remctld_start_internal(struct kerberos_config *krbconf, const char *config,
141141
* Just calls remctld_start_internal without enabling fakeroot support.
142142
*/
143143
struct process *
144-
remctld_start(struct kerberos_config *krbconf, const char *config, ...)
144+
remctld_start(const struct kerberos_config *krbconf, const char *config, ...)
145145
{
146146
va_list args;
147147
struct process *process;
@@ -157,8 +157,8 @@ remctld_start(struct kerberos_config *krbconf, const char *config, ...)
157157
* Just calls remctld_start_internal with fakeroot enabled.
158158
*/
159159
struct process *
160-
remctld_start_fakeroot(struct kerberos_config *krbconf, const char *config,
161-
...)
160+
remctld_start_fakeroot(const struct kerberos_config *krbconf,
161+
const char *config, ...)
162162
{
163163
va_list args;
164164
struct process *process;

tests/tap/remctl.h

+5-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* which can be found at <https://www.eyrie.org/~eagle/software/rra-c-util/>.
99
*
1010
* Written by Russ Allbery <[email protected]>
11+
* Copyright 2024 Russ Allbery <[email protected]>
1112
* Copyright 2006-2007, 2009, 2011-2013
1213
* The Board of Trustees of the Leland Stanford Junior University
1314
*
@@ -64,15 +65,16 @@ BEGIN_DECLS
6465
* config.h. If it's not defined, remctld_start calls skip_all, assuming that
6566
* this means that the test case cannot be run.
6667
*/
67-
struct process *remctld_start(struct kerberos_config *, const char *config,
68-
...) __attribute__((__nonnull__(1, 2)));
68+
struct process *remctld_start(const struct kerberos_config *,
69+
const char *config, ...)
70+
__attribute__((__nonnull__(1, 2)));
6971

7072
/*
7173
* Like remctld_start, but run remctld under fakeroot. Calls skip_all if
7274
* PATH_FAKEROOT is not defined, either with explicit compiler options or in
7375
* config.h.
7476
*/
75-
struct process *remctld_start_fakeroot(struct kerberos_config *,
77+
struct process *remctld_start_fakeroot(const struct kerberos_config *,
7678
const char *config, ...)
7779
__attribute__((__nonnull__(1, 2)));
7880

util/buffer.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* which can be found at <https://www.eyrie.org/~eagle/software/rra-c-util/>.
1717
*
1818
* Written by Russ Allbery <[email protected]>
19-
* Copyright 2015-2016, 2019 Russ Allbery <[email protected]>
19+
* Copyright 2015-2016, 2019, 2024 Russ Allbery <[email protected]>
2020
* Copyright 2011-2012, 2014
2121
* The Board of Trustees of the Leland Stanford Junior University
2222
* Copyright 2004-2006 Internet Systems Consortium, Inc. ("ISC")
@@ -241,7 +241,8 @@ bool
241241
buffer_find_string(struct buffer *buffer, const char *string, size_t start,
242242
size_t *offset)
243243
{
244-
char *terminator, *data;
244+
char *data;
245+
const char *terminator;
245246
size_t length;
246247

247248
if (buffer->data == NULL)

util/network.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
* which can be found at <https://www.eyrie.org/~eagle/software/rra-c-util/>.
2121
*
2222
* Written by Russ Allbery <[email protected]>
23-
* Copyright 2014-2017 Russ Allbery <[email protected]>
23+
* Copyright 2014-2017, 2024 Russ Allbery <[email protected]>
2424
* Copyright 2009, 2011-2014
2525
* The Board of Trustees of the Leland Stanford Junior University
2626
* Copyright 2004-2008 Internet Systems Consortium, Inc. ("ISC")
@@ -278,7 +278,8 @@ bool
278278
network_bind_all(int type, unsigned short port, socket_type **fds,
279279
unsigned int *count)
280280
{
281-
struct addrinfo hints, *addrs, *addr;
281+
struct addrinfo hints, *addrs;
282+
const struct addrinfo *addr;
282283
unsigned int size;
283284
int status;
284285
socket_type fd;

0 commit comments

Comments
 (0)