Skip to content

tests/gnrc_udp: Replace atoi() by strtol().#11356

Merged
miri64 merged 2 commits intoRIOT-OS:masterfrom
jcarrano:fix_udp_atoi-master
Apr 11, 2019
Merged

tests/gnrc_udp: Replace atoi() by strtol().#11356
miri64 merged 2 commits intoRIOT-OS:masterfrom
jcarrano:fix_udp_atoi-master

Conversation

@jcarrano
Copy link
Copy Markdown
Contributor

@jcarrano jcarrano commented Apr 8, 2019

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 term

In the RIOT terminal, try to send a 0 length packet:

> udp send fe80::bd:b7e 1337 0
udp send fe80::bd:b7e 1337 0
Error: unable to parse data_len

With this patch:

> udp send fe80::bd:b7e 1337 0
udp send fe80::bd:b7e 1337 0
Success: send 0 byte to [fe80::bd:b7e]:1337

Issues/PRs references

None.

@jcarrano jcarrano added Type: bug The issue reports a bug / The PR fixes a bug (including spelling errors) Area: tests Area: tests and testing framework CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR Process: needs backport Integration Process: The PR is required to be backported to a release or feature branch labels Apr 8, 2019
@jcarrano jcarrano requested a review from miri64 April 8, 2019 16:10
Copy link
Copy Markdown
Member

@miri64 miri64 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 ;-).

if (argc > 5) {
num = strtol(argv[5], &conversion_end, 0);
if (*conversion_end != '\0') {
goto send_syntax_err;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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') {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@miri64
Copy link
Copy Markdown
Member

miri64 commented Apr 10, 2019

Ping @jcarrano?

@jcarrano jcarrano force-pushed the fix_udp_atoi-master branch from b860a31 to 54cbdaa Compare April 10, 2019 13:30
@jcarrano
Copy link
Copy Markdown
Contributor Author

@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.
Copy link
Copy Markdown
Member

@miri64 miri64 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK

@miri64
Copy link
Copy Markdown
Member

miri64 commented Apr 10, 2019

Murdocks complaining about an unrelated error here

printf("Packets received: %d\n", ++rcv_count);
. Can you please piggy-back a fix?

This was causing the CI build to fail in the static-check stage
(cppcheck).
@miri64 miri64 merged commit 364499f into RIOT-OS:master Apr 11, 2019
@miri64
Copy link
Copy Markdown
Member

miri64 commented Apr 11, 2019

Please adapt your backport.

@miri64 miri64 added this to the Release 2019.04 milestone Apr 11, 2019
@danpetry danpetry removed this from the Release 2019.04 milestone Apr 26, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: tests Area: tests and testing framework CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR Process: needs backport Integration Process: The PR is required to be backported to a release or feature branch Type: bug The issue reports a bug / The PR fixes a bug (including spelling errors)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants