Skip to content

Commit

Permalink
perftests/{recv,send}-zeros: fix sanity checks
Browse files Browse the repository at this point in the history
I added sanity checks in:

    2022-08-23 perfests/{recv,send}-zeros: add sanity checks
    c87a40b

which were a bit confused (i.e. checking that a (size_t) was not <= 0).
At that time:

- buflen in recv-zeros/main.c was a (ssize_t), whereas
- buflen in send-zeros/main.c was a (size_t), and
- clang-tidy 14 and lower could not follow our PARSENUM().
  macro to understand that:

    if (PARSENUM(&buflen, argv[2], 1, SSIZE_MAX))
            goto err0;

  would always result in a positive, non-zero value in buflen.

This commit:

- makes both files use (size_t)buflen.
- adds bounds to PARSENUM(&count...
- eliminates the post-PARSENUM sanity checks.

  That last point will make clang-tidy versions below 15.0.0 (2022-09-6)
  complain about the code, but inspection shows that it's correct.
  And it's normal for clang-tidy to have tons of complaints anyway;
  I normally disable ~60 types of warnings.

- also, this commit cleans up the #includes.
  • Loading branch information
gperciva committed Sep 6, 2023
1 parent a14e394 commit 24c9173
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 23 deletions.
15 changes: 4 additions & 11 deletions perftests/recv-zeros/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

Expand All @@ -15,7 +14,7 @@ main(int argc, char ** argv)
{
/* Command-line parameters. */
const char * addr = NULL;
ssize_t buflen;
size_t buflen;

/* Working variables. */
struct sock_addr * sa;
Expand All @@ -37,14 +36,8 @@ main(int argc, char ** argv)
goto err0;
}

/* Sanity check. */
if (buflen <= 0) {
warnp("Can't have a buffer length <= 0!");
goto err0;
}

/* Allocate buffer. */
if ((buffer = malloc((size_t)buflen)) == NULL) {
if ((buffer = malloc(buflen)) == NULL) {
warnp("malloc");
goto err0;
}
Expand Down Expand Up @@ -76,8 +69,8 @@ main(int argc, char ** argv)

/* Receive data, but do nothing with it. */
do {
r = recv(socket_recv, buffer, (size_t)buflen, MSG_WAITALL);
} while (r == buflen);
r = recv(socket_recv, buffer, buflen, MSG_WAITALL);
} while (r == (ssize_t)buflen);
if (r != 0) {
warnp("recv");
goto err4;
Expand Down
14 changes: 2 additions & 12 deletions perftests/send-zeros/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

#include <fcntl.h>
#include <limits.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

#include "monoclock.h"
Expand Down Expand Up @@ -42,21 +42,11 @@ main(int argc, char ** argv)
warnp("parsenum");
goto err0;
}
if (PARSENUM(&count, argv[3])) {
if (PARSENUM(&count, argv[3], 1, SIZE_MAX)) {
warnp("parsenum");
goto err0;
}

/* Sanity check. */
if (buflen <= 0) {
warnp("Can't have a buffer length <= 0!");
goto err0;
}
if (count == 0) {
warnp("Can't send 0 blocks!");
goto err0;
}

/* Allocate and fill buffer to send. */
if ((buffer = malloc(buflen)) == NULL) {
warnp("Out of memory");
Expand Down

0 comments on commit 24c9173

Please sign in to comment.