Skip to content

Commit 3ff3104

Browse files
committed
tests: test code tweaks
Be more cautious on unexpected failures and input: binder Avoid returning garbage value from binder_parse() in case of an unexpected (impossible?) empty buffer. Store create_bpf_*() results temporarily in an int to actually perform the error checks (they are currently no-ops on unsigned). bpf Initialize variable in case the program gets called without the associated option. cap_userns Use appropriate types and casts to avoid implicit conversions. execshare Avoid use of void pointer arithmetic. fdreceive Do not call non async-safe exit(3) in signal handler. Drop dead assignment. filesystem Initialize variables in case the programs get called without the associated options. inet_socket/unix_socket Declare usage() as noreturn to help compilers avoid issuing inaccurate warnings. inherit Use a large enough buffer for a potential huge PID. key_socket Avoid comparison of signed with unsigned integer. module_load Correctly check for an open(2) failure. nnp_nosuid Check if wait(2) succeeded before checking the child status. notify Check if opening file was successful. Use appropriate type for read(2) return value. prlimit Set all members of the new limit structure. sctp Use appropriate iterator type. Signed-off-by: Christian Göttsche <[email protected]>
1 parent 92a7bff commit 3ff3104

File tree

21 files changed

+36
-24
lines changed

21 files changed

+36
-24
lines changed

tests/binder/client.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ static void extract_handle_and_acquire(int fd,
231231
static int binder_parse(int fd, binder_uintptr_t ptr, binder_size_t size)
232232
{
233233
binder_uintptr_t end = ptr + size;
234-
uint32_t cmd;
234+
uint32_t cmd = BR_DEAD_REPLY;
235235

236236
while (ptr < end) {
237237
cmd = *(uint32_t *)ptr;

tests/binder/manager.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ static void reply_with_handle(int fd, struct binder_transaction_data *txn_in)
156156
static int binder_parse(int fd, binder_uintptr_t ptr, binder_size_t size)
157157
{
158158
binder_uintptr_t end = ptr + size;
159-
uint32_t cmd;
159+
uint32_t cmd = BR_DEAD_REPLY;
160160

161161
while (ptr < end) {
162162
cmd = *(uint32_t *)ptr;

tests/binder/service_provider.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,16 @@ static void request_service_provider_fd(int fd,
7676
break;
7777
#if HAVE_BPF
7878
case BPF_MAP_FD:
79-
obj.fd = create_bpf_map();
80-
if (obj.fd < 0)
79+
result = create_bpf_map();
80+
if (result < 0)
8181
exit(70);
82+
obj.fd = result;
8283
break;
8384
case BPF_PROG_FD:
84-
obj.fd = create_bpf_prog();
85-
if (obj.fd < 0)
85+
result = create_bpf_prog();
86+
if (result < 0)
8687
exit(71);
88+
obj.fd = result;
8789
break;
8890
#else
8991
case BPF_MAP_FD:
@@ -122,7 +124,7 @@ static void request_service_provider_fd(int fd,
122124
static int binder_parse(int fd, binder_uintptr_t ptr, binder_size_t size)
123125
{
124126
binder_uintptr_t end = ptr + size;
125-
uint32_t cmd;
127+
uint32_t cmd = BR_DEAD_REPLY;
126128

127129
while (ptr < end) {
128130
cmd = *(uint32_t *)ptr;

tests/bpf/bpf_test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ int main(int argc, char *argv[])
2020
enum {
2121
MAP_FD = 1,
2222
PROG_FD
23-
} bpf_fd_type;
23+
} bpf_fd_type = -1;
2424

2525
while ((opt = getopt(argc, argv, "mpv")) != -1) {
2626
switch (opt) {

tests/cap_userns/userns_child_exec.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ usage(char *pname)
8989
static void
9090
update_map(char *mapping, char *map_file)
9191
{
92-
int fd, j;
93-
size_t map_len; /* Length of 'mapping' */
92+
int fd;
93+
size_t j, map_len; /* Length of 'mapping' */
9494

9595
/* Replace commas in mapping string with newlines */
9696

@@ -106,7 +106,7 @@ update_map(char *mapping, char *map_file)
106106
exit(EXIT_FAILURE);
107107
}
108108

109-
if (write(fd, mapping, map_len) != map_len) {
109+
if (write(fd, mapping, map_len) != (ssize_t)map_len) {
110110
fprintf(stderr, "ERROR: write %s: %s\n", map_file,
111111
strerror(errno));
112112
exit(EXIT_FAILURE);

tests/execshare/parent.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ int main(int argc, char **argv)
4343
perror("malloc");
4444
exit(-1);
4545
}
46-
clone_stack = page + pagesize;
46+
clone_stack = (unsigned char *)page + pagesize;
4747

4848
rc = getcon(&context_tmp);
4949
if (rc < 0) {

tests/fdreceive/server.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include <stdlib.h>
1010

1111
char my_path[1024];
12-
#define CLEANUP_AND_EXIT do { unlink(my_path); exit(1); } while (0)
12+
#define CLEANUP_AND_EXIT do { unlink(my_path); _exit(1); } while (0)
1313

1414
void handler(int sig)
1515
{
@@ -43,7 +43,6 @@ int main(int argc, char **argv)
4343
}
4444

4545
sun.sun_family = AF_UNIX;
46-
sunlen = sizeof(struct sockaddr_un);
4746
strcpy(sun.sun_path, argv[2]);
4847
sunlen = strlen(sun.sun_path) + 1 + sizeof(short);
4948
strcpy(my_path, sun.sun_path);

tests/filesystem/fs_relabel.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ int main(int argc, char **argv)
2727
{
2828
int opt, result, save_err;
2929
const char *newcon;
30-
char *context, *fs_con = NULL, *base_dir, *type;
30+
char *context, *fs_con = NULL, *base_dir = NULL, *type = NULL;
3131
char fs_mount[PATH_MAX];
3232
bool verbose = false;
3333
context_t con_t;

tests/filesystem/grim_reaper.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ int main(int argc, char *argv[])
2626
size_t len;
2727
ssize_t num;
2828
int opt, index = 0, i, result = 0;
29-
char *mount_info[2], *buf = NULL, *item, *tgt;
29+
char *mount_info[2], *buf = NULL, *item, *tgt = NULL;
3030
bool verbose = false;
3131

3232
while ((opt = getopt(argc, argv, "t:v")) != -1) {

tests/inet_socket/bind.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#define IPPROTO_MPTCP 262
1313
#endif
1414

15+
__attribute__((noreturn))
1516
void usage(char *progname)
1617
{
1718
fprintf(stderr, "usage: %s protocol port\n", progname);

0 commit comments

Comments
 (0)