tests/gnrc_udp: Replace atoi() by strtol().#11356
Conversation
miri64
left a comment
There was a problem hiding this comment.
Weird error handling, but apparently it is the only way with that function.
However, please only use goto if there is no other elegant way. Jumping into the middle of the function is not at all elegant ;-).
tests/gnrc_udp/udp.c
Outdated
| if (argc > 5) { | ||
| num = strtol(argv[5], &conversion_end, 0); | ||
| if (*conversion_end != '\0') { | ||
| goto send_syntax_err; |
There was a problem hiding this comment.
Rather use a _usage() function here and just return 1. Jumping into the middle of some if-handling just invites spaghetti code.
| data_len = atoi(data_len_str); | ||
| if (data_len == 0) { | ||
| data_len = strtoul(data_len_str, &conversion_end, 0); | ||
| if (*conversion_end != '\0') { |
There was a problem hiding this comment.
Sleeping over it, let's just focus on fixing this one for now for the release. The other stuff should also be fixed in applications like examples/gnrc_networking
|
Ping @jcarrano? |
b860a31 to
54cbdaa
Compare
|
@miri64 I stripped down the patch. |
This patch is a reduced version of an earlier one, with the bare minimum required to be able to run the test and get the release going. Original description: atoi() cannot detect errors. Many implementation return zero on error and that is what was being checked here, making the "udp send" command unable to parse integer values of zero. On top of this, the behavior on errors does not seem to be specified in the standard (so it is not even correct to check for zero even when zero is not an accepted value, like for a port number). The result of all this is that sending UDP packets of zero length (as required by the Release Specs) was not possible. This patch replaces atoi by strlen, which allows for robust error detection. Sending zero length packets is possible.
|
Murdocks complaining about an unrelated error here Line 62 in e0de94c |
This was causing the CI build to fail in the static-check stage (cppcheck).
|
Please adapt your backport. |
Contribution description
atoi() cannot detect errors. Many implementation return zero on error and that is what was being checked here, making the "udp send" command unable to parse integer values of zero. On top of this, the behavior on errors does not seem to be specified in the standard (so it is not even correct to check for zero even when zero is not an accepted value, like for a port number).
The result of all this is that sending UDP packets of zero length (as required by the Release Specs) was not possible.
This patch replaces atoi by strlen, which allows for robust error detection. Sending zero length packets is possible.
Testing procedure
Run
make -C tests/gnrc_udp all termIn the RIOT terminal, try to send a 0 length packet:
With this patch:
Issues/PRs references
None.