Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

posix_socket: initialize uninitialized fields #11575

Merged
merged 1 commit into from
Aug 8, 2019

Conversation

miri64
Copy link
Member

@miri64 miri64 commented May 24, 2019

Contribution description

Some fields of _sock_tl_ep were not initialized when converting a sockaddr to a _sock_tl_ep. Though currently the only missing field is netif it is safer to use memset() here, to make the implementation more future proof.

Testing procedure

Flash and run the application provided in #11212 on 2 samr21-xpros. Without this PR, the echo send command will report errno 22 (EINVAL), with it it will report EBADF (or crash if #11212 (comment) is not applied).

Issues/PRs references

Addresses #11212 but does not fix it, as that issue contains multiple bugs.

@miri64 miri64 added Type: bug The issue reports a bug / The PR fixes a bug (including spelling errors) Area: POSIX Area: POSIX API wrapper Area: network Area: Networking labels May 24, 2019
@miri64 miri64 added this to the Release 2019.07 milestone May 24, 2019
@miri64 miri64 requested a review from kaspar030 May 24, 2019 13:16
smlng
smlng previously requested changes May 29, 2019
@@ -221,6 +221,7 @@ static int _sockaddr_to_ep(const struct sockaddr *address, socklen_t address_len
return -1;
}
struct sockaddr_in *in_addr = (struct sockaddr_in *)address;
memset(out, 0, *out);
Copy link
Member

Choose a reason for hiding this comment

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

I would rather leave this to the caller of this (internal) function, i.e., the calling function should init the struct as needed.

Copy link
Member

Choose a reason for hiding this comment

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

thereby also avoiding the separate memset for ipv4 and ipv6 here.

Copy link
Member Author

@miri64 miri64 Jun 4, 2019

Choose a reason for hiding this comment

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

But multiplying the memset for every call of this function (which is 4).

sys/posix/sockets/posix_sockets.c:static int _sockaddr_to_ep(const struct sockaddr *address, socklen_t address_len,
sys/posix/sockets/posix_sockets.c:    if (_sockaddr_to_ep(address, address_len, &s->local) < 0) {
sys/posix/sockets/posix_sockets.c:        if (_sockaddr_to_ep(address, address_len, &r) < 0) {
sys/posix/sockets/posix_sockets.c:    if ((res = _sockaddr_to_ep(address, address_len, &ep)) < 0)
sys/posix/sockets/posix_sockets.c:                res = _sockaddr_to_ep(address, address_len, &ep);

So I think having it this way (for this internal function) is more memory efficient.

Copy link
Contributor

Choose a reason for hiding this comment

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

rather use memset(out, 0, sizeof(*out)));?!

Copy link
Member Author

Choose a reason for hiding this comment

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

Oops Oo

@miri64
Copy link
Member Author

miri64 commented Jun 19, 2019

Ping?

@miri64
Copy link
Member Author

miri64 commented Jul 30, 2019

Ping @smlng?

Copy link
Contributor

@haukepetersen haukepetersen left a comment

Choose a reason for hiding this comment

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

sizeof missing?!

@@ -221,6 +221,7 @@ static int _sockaddr_to_ep(const struct sockaddr *address, socklen_t address_len
return -1;
}
struct sockaddr_in *in_addr = (struct sockaddr_in *)address;
memset(out, 0, *out);
Copy link
Contributor

Choose a reason for hiding this comment

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

rather use memset(out, 0, sizeof(*out)));?!

Copy link
Contributor

@haukepetersen haukepetersen left a comment

Choose a reason for hiding this comment

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

ACK

@smlng I think the memset here serves a good purpose and adds some security. Of course there is overhead involved, but we are talking about a small chunk of memory to set, so its a function call + minimal runtime. I think its processing time well spend.

@MrKevinWeiss
Copy link
Contributor

@smlng I think it looks good now.

@miri64 please squash!

@MrKevinWeiss MrKevinWeiss added the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label Aug 8, 2019
@miri64 miri64 force-pushed the posix_socket/fix/init-uninit-fields branch from 33d587c to 3bcb1ee Compare August 8, 2019 13:03
@miri64
Copy link
Member Author

miri64 commented Aug 8, 2019

Squashed.

@smlng smlng dismissed their stale review August 8, 2019 13:23

won't block

@miri64
Copy link
Member Author

miri64 commented Aug 8, 2019

Thanks @smlng, @haukepetersen, and @MrKevinWeiss for your review.

@miri64 miri64 merged commit af5bf36 into RIOT-OS:master Aug 8, 2019
@miri64 miri64 deleted the posix_socket/fix/init-uninit-fields branch August 8, 2019 13:56
@kb2ma kb2ma added this to the Release 2019.10 milestone Sep 16, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: network Area: Networking Area: POSIX Area: POSIX API wrapper CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR 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.

5 participants