Skip to content

Commit 5725d5d

Browse files
committed
Fix building with NDEBUG
This disables `assert()` and thus caused some warnings/errors and missing code. Add a test case to cover this mode.
1 parent 7c0671c commit 5725d5d

File tree

5 files changed

+80
-29
lines changed

5 files changed

+80
-29
lines changed

src/ioctl_tree.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,12 +243,12 @@ ioctl_tree_read(FILE * f)
243243
void
244244
ioctl_tree_write(FILE * f, const ioctl_tree * tree)
245245
{
246-
int i;
246+
int res;
247247
if (tree == NULL)
248248
return;
249249

250250
/* write indent */
251-
for (i = 0; i < tree->depth; ++i)
251+
for (int i = 0; i < tree->depth; ++i)
252252
fputc(' ', f);
253253
if (tree->id != tree->type->id) {
254254
long offset;
@@ -260,7 +260,8 @@ ioctl_tree_write(FILE * f, const ioctl_tree * tree)
260260
fprintf(f, "%s %i ", tree->type->name, tree->ret);
261261
}
262262
tree->type->write(tree, f);
263-
assert(fputc('\n', f) == '\n');
263+
res = fputc('\n', f);
264+
assert(res == '\n');
264265

265266
ioctl_tree_write(f, tree->child);
266267
ioctl_tree_write(f, tree->next);

src/libumockdev-preload.c

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,8 @@ ioctl_record_open(int fd)
452452

453453
/* lazily open the record file */
454454
if (ioctl_record_log == NULL) {
455+
int r;
456+
455457
const char *path = getenv("UMOCKDEV_IOCTL_RECORD_FILE");
456458
const char *device_path = getenv("UMOCKDEV_IOCTL_RECORD_DEVICE_PATH");
457459
struct sigaction act_int;
@@ -514,9 +516,11 @@ ioctl_record_open(int fd)
514516

515517
/* ensure that we write the file also on Control-C */
516518
act_int.sa_handler = ioctl_record_sigint_handler;
517-
assert(sigemptyset(&act_int.sa_mask) == 0);
519+
r = sigemptyset(&act_int.sa_mask);
520+
assert(r == 0);
518521
act_int.sa_flags = 0;
519-
assert(sigaction(SIGINT, &act_int, &orig_actint) == 0);
522+
r = sigaction(SIGINT, &act_int, &orig_actint);
523+
assert(r == 0);
520524

521525
DBG(DBG_IOCTL, "ioctl_record_open: starting ioctl recording of fd %i into %s\n", fd, path);
522526
} else {
@@ -535,8 +539,11 @@ ioctl_record_close(int fd)
535539

536540
/* recorded anything? */
537541
if (ioctl_record != NULL) {
542+
int r;
543+
538544
rewind(ioctl_record_log);
539-
assert(ftruncate(fileno(ioctl_record_log), 0) == 0);
545+
r = ftruncate(fileno(ioctl_record_log), 0);
546+
assert(r == 0);
540547
fprintf(ioctl_record_log, "@DEV %s\n", getenv("UMOCKDEV_IOCTL_RECORD_DEVICE_PATH"));
541548
ioctl_tree_write(ioctl_record_log, ioctl_record);
542549
fflush(ioctl_record_log);
@@ -545,9 +552,12 @@ ioctl_record_close(int fd)
545552

546553
static void ioctl_record_sigint_handler(int signum)
547554
{
555+
int r;
556+
548557
DBG(DBG_IOCTL, "ioctl_record_sigint_handler: got signal %i, flushing record\n", signum);
549558
ioctl_record_close(ioctl_record_fd);
550-
assert(sigaction(SIGINT, &orig_actint, NULL) == 0);
559+
r = sigaction(SIGINT, &orig_actint, NULL);
560+
assert(r == 0);
551561
raise(signum);
552562
}
553563

@@ -851,7 +861,10 @@ script_start_record(int fd, const char *logname, const char *recording_path, enu
851861

852862
srinfo = malloc(sizeof(struct script_record_info));
853863
srinfo->log = log;
854-
assert(clock_gettime(CLOCK_MONOTONIC, &srinfo->time) == 0);
864+
if (clock_gettime(CLOCK_MONOTONIC, &srinfo->time) < 0) {
865+
fprintf(stderr, "libumockdev-preload: failed to clock_gettime: %m\n");
866+
abort();
867+
}
855868
srinfo->op = 0;
856869
srinfo->fmt = fmt;
857870
fd_map_add(&script_recorded_fds, fd, srinfo);
@@ -861,9 +874,10 @@ static void
861874
script_record_open(int fd)
862875
{
863876
dev_t fd_dev;
864-
const char *logname, *recording_path;
865-
const void* data;
877+
const char *logname, *recording_path = NULL;
878+
const void* data = NULL;
866879
enum script_record_format fmt;
880+
int r;
867881

868882
if (!script_dev_logfile_map_inited)
869883
init_script_dev_logfile_map();
@@ -874,8 +888,10 @@ script_record_open(int fd)
874888
DBG(DBG_SCRIPT, "script_record_open: fd %i on device %i:%i is not recorded\n", fd, major(fd_dev), minor(fd_dev));
875889
return;
876890
}
877-
assert (fd_map_get(&script_dev_devpath_map, fd_dev, (const void **)&recording_path));
878-
assert (fd_map_get(&script_dev_format_map, fd_dev, &data));
891+
r = fd_map_get(&script_dev_devpath_map, fd_dev, (const void **)&recording_path);
892+
assert(r);
893+
r = fd_map_get(&script_dev_format_map, fd_dev, &data);
894+
assert(r);
879895
fmt = (enum script_record_format) data;
880896

881897
DBG(DBG_SCRIPT, "script_record_open: start recording fd %i on device %i:%i into %s (format %i)\n",
@@ -924,7 +940,10 @@ update_msec(struct timespec *tm)
924940
{
925941
struct timespec now;
926942
long delta;
927-
assert(clock_gettime(CLOCK_MONOTONIC, &now) == 0);
943+
if (clock_gettime(CLOCK_MONOTONIC, &now) < 0) {
944+
fprintf(stderr, "libumockdev-preload: failed to clock_gettime: %m\n");
945+
abort();
946+
}
928947
delta = (now.tv_sec - tm->tv_sec) * 1000 + now.tv_nsec / 1000000 - tm->tv_nsec / 1000000;
929948
assert(delta >= 0);
930949
*tm = now;
@@ -940,7 +959,7 @@ script_record_op(char op, int fd, const void *buf, ssize_t size)
940959
libc_func(fwrite, size_t, const void *, size_t, size_t, FILE *);
941960
static char header[100];
942961
const unsigned char *cur;
943-
int i;
962+
int i, r;
944963

945964
if (!fd_map_get(&script_recorded_fds, fd, (const void **)&srinfo))
946965
return;
@@ -959,7 +978,8 @@ script_record_op(char op, int fd, const void *buf, ssize_t size)
959978
if (srinfo->op != 0)
960979
putc('\n', srinfo->log);
961980
snprintf(header, sizeof(header), "%c %lu ", op, delta);
962-
assert(_fwrite(header, strlen(header), 1, srinfo->log) == 1);
981+
r = _fwrite(header, strlen(header), 1, srinfo->log);
982+
assert(r == 1);
963983
}
964984

965985
/* escape ASCII control chars */

tests/chatter-socket-stream.c

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,21 @@
2323
#include <assert.h>
2424
#include <fcntl.h>
2525
#include <unistd.h>
26+
#include <stdlib.h>
2627
#include <string.h>
2728
#include <sys/socket.h>
2829
#include <sys/un.h>
2930

30-
#define writestr(s) assert(write(fd, s, strlen(s)) >= 0)
31+
static void
32+
writestr (int fd, const char *s)
33+
{
34+
int r;
35+
r = write(fd, s, strlen(s));
36+
if (r <= 0) {
37+
perror ("write");
38+
abort();
39+
}
40+
}
3141

3242
int
3343
main(int argc, char **argv)
@@ -56,17 +66,18 @@ main(int argc, char **argv)
5666
return 1;
5767
}
5868

59-
writestr("What is your name?\n");
69+
writestr(fd, "What is your name?\n");
6070
len = read(fd, buf, sizeof(buf) - 1);
6171
assert(len >= 0);
6272
buf[len] = 0;
6373
printf("Got name: %s\n", buf);
64-
writestr("hello ");
65-
writestr(buf);
74+
writestr(fd, "hello ");
75+
writestr(fd, buf);
6676

6777
/* test send/recv, too */
6878
usleep(20000);
69-
assert(send(fd, "send()", 6, 0) == 6);
79+
len = send(fd, "send()", 6, 0);
80+
assert(len == 6);
7081

7182
len = recv(fd, buf, sizeof(buf) - 1, 0);
7283
assert(len > 0);

tests/chatter.c

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,22 @@
1919
*/
2020

2121
#include <stdio.h>
22+
#include <stdlib.h>
2223
#include <assert.h>
2324
#include <fcntl.h>
2425
#include <unistd.h>
2526
#include <string.h>
2627

27-
#define writestr(s) assert(write(fd, s, strlen(s)) >= 0)
28+
static void
29+
writestr (int fd, const char *s)
30+
{
31+
int r;
32+
r = write(fd, s, strlen(s));
33+
if (r <= 0) {
34+
perror ("write");
35+
abort();
36+
}
37+
}
2838

2939
int
3040
main(int argc, char **argv)
@@ -43,20 +53,20 @@ main(int argc, char **argv)
4353
perror("open:");
4454
return 1;
4555
}
46-
writestr("Hello world!\n");
47-
writestr("What is your name?\n");
56+
writestr(fd, "Hello world!\n");
57+
writestr(fd, "What is your name?\n");
4858
len = read(fd, buf, sizeof(buf) - 1);
4959
assert(len >= 0);
5060
buf[len] = 0;
5161
printf("Got input: %s", buf);
52-
writestr("I ♥ ");
53-
writestr(buf);
54-
writestr("a\t tab and a\n line break in one write\n");
62+
writestr(fd, "I ♥ ");
63+
writestr(fd, buf);
64+
writestr(fd, "a\t tab and a\n line break in one write\n");
5565
len = read(fd, buf, sizeof(buf) - 1);
5666
assert(len >= 0);
5767
buf[len] = 0;
5868
printf("Got input: %s", buf);
59-
writestr("bye!\n");
69+
writestr(fd, "bye!\n");
6070
close(fd);
6171
return 0;
6272
}

tests/run-ubuntu-chroot

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ trap "pkill -e -kill Xorg || true" EXIT INT QUIT PIPE
5959
set -ex
6060
cd /build/src
6161
./autogen.sh --prefix=/usr
62-
make -j4
62+
make -j$(nproc)
6363
make check-valgrind
64-
make -j4 distcheck
64+
make -j$(nproc) distcheck
6565
EOU
6666
6767
# install and run check-installed
@@ -74,5 +74,14 @@ set -ex
7474
cd /build/src
7575
make check-installed
7676
EOU
77+
78+
# check build without assertions
79+
su - buildd <<EOU
80+
set -ex
81+
cd /build/src
82+
make distclean
83+
./autogen.sh --prefix=/usr CPPFLAGS=-DNDEBUG
84+
make -j$(nproc) check
85+
EOU
7786
EOF
7887
cp "$CHROOT"/build/src/umockdev-*.tar.xz .

0 commit comments

Comments
 (0)