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_sockets.c: Fix 2 byte int compilation errors #19946

Merged
merged 1 commit into from Sep 29, 2023

Conversation

mrdeep1
Copy link
Contributor

@mrdeep1 mrdeep1 commented Sep 25, 2023

Contribution description

When building with POSIX_SETSOCKOPT defined (to be able to use the posix setsockopt() function to set SO_RCVTIMEO), this code fails to build with 16 bit integer compilers for some of the boards as there is an integer overflow for 1000*1000.

There is also an invalid test that the provided tv->tv_sec is less than 0 as it is a uint32_t.

Testing procedure

Murdock should fail if the following change is made to tests/sys/posix_sleep/Makefile without this PR.

diff --git a/tests/sys/posix_sleep/Makefile b/tests/sys/posix_sleep/Makefile
index 5dcb9ed74c..9e4f881492 100644
--- a/tests/sys/posix_sleep/Makefile
+++ b/tests/sys/posix_sleep/Makefile
@@ -5,4 +5,9 @@ USEMODULE += posix_sleep
 # microbit qemu failing currently
 TEST_ON_CI_BLACKLIST += microbit

+USEMODULE += gnrc_ipv6_default
+USEMODULE += sock_udp
+USEMODULE += posix_sockets
+CFLAGS += -DPOSIX_SETSOCKOPT
+
 include $(RIOTBASE)/Makefile.include

Issues/PRs references

None.

@github-actions github-actions bot added the Area: sys Area: System label Sep 25, 2023
@benpicco benpicco added the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label Sep 26, 2023
@@ -1121,7 +1121,7 @@ int setsockopt(int socket, int level, int option_name, const void *option_value,
#ifdef POSIX_SETSOCKOPT
socket_t *s;
struct timeval *tv;
const uint32_t max_timeout_secs = UINT32_MAX / (1000 * 1000);
const uint32_t max_timeout_secs = UINT32_MAX / (1000UL * 1000);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
const uint32_t max_timeout_secs = UINT32_MAX / (1000UL * 1000);
const uint32_t max_timeout_secs = UINT32_MAX / US_PER_SEC;

should make it clearer what's going on (from time_units.h)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated as suggested.

Copy link
Contributor

Choose a reason for hiding this comment

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

Did you push your changes?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not yet - thinking about the time_t definition issue for avr cpus.

@@ -1144,7 +1144,8 @@ int setsockopt(int socket, int level, int option_name, const void *option_value,

tv = (struct timeval *) option_value;

if (tv->tv_sec < 0 || tv->tv_usec < 0) {
/* tv_sec is unit32_t, so never negative */
Copy link
Contributor

Choose a reason for hiding this comment

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

Are you sure? According to the standard, it's time_t, which is usually int64_t these days.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree that the standard refers to time_t, and the 3 definitions of tv_sec all refer to time_t in RIOT as per

  # pri kind tag               file
  1 F C m    tv_sec            ./cpu/esp8266/include/sys/types.h
               struct:timespec
               time_t  tv_sec;   /* Seconds */
  2 F   m    tv_sec            ./cpu/avr8_common/avr_libc_extra/include/sys/time.h
               struct:timeval
               time_t         tv_sec;      /**< seconds */
  3 F   m    tv_sec            ./cpu/avr8_common/avr_libc_extra/include/vendor/time.h
               struct:timespec
               time_t    tv_sec;

However, in RIOT, in the 3 places where time_t is defined we have

  # pri kind tag               file
  1 F C t    time_t            ./cpu/esp8266/include/sys/types.h
               typedef _TIME_T_ time_t;
  2 F   t    time_t            ./cpu/avr8_common/avr_libc_extra/include/sys/types.h
               typedef     uint32_t time_t;      /**< Used for time in seconds */
  3 F   t    time_t            ./cpu/avr8_common/avr_libc_extra/include/vendor/time.h
               typedef uint32_t time_t;

but _TIME_T_ is not defined anywhere.

So it looks like this change will have to be some sort of conditional based on an AVR build, but haven't worked out which conditional to use yet.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah so you are getting a build warning (that gets promited to error) on AVR with this check in place?

Copy link
Contributor

@benpicco benpicco Sep 26, 2023

Choose a reason for hiding this comment

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

I'd be fine with dropping that check if it makes things easier, it might as well have been an assert().

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Without this PR, the following is reported (assuming POSIX_SETSOCKOPT is defined and building for AVR)

/home/jon/RIOT/sys/posix/sockets/posix_sockets.c:1147:20: error: comparison of unsigned expression < 0 is always false [-Werror=type-limits]
     if (tv->tv_sec < 0 || tv->tv_usec < 0) {
                    ^
cc1: all warnings being treated as errors

@benpicco benpicco added the Type: bug The issue reports a bug / The PR fixes a bug (including spelling errors) label Sep 26, 2023
@riot-ci
Copy link

riot-ci commented Sep 26, 2023

Murdock results

✔️ PASSED

ed64f06 posix_sockets.c: Fix 16 bit integer compilation errors

Success Failures Total Runtime
7937 0 7937 15m:04s

Artifacts

@mrdeep1
Copy link
Contributor Author

mrdeep1 commented Sep 26, 2023

Changes pushed as a separate commit.

Before and after this PR change can be tested with this additional change

diff --git a/examples/posix_sockets/Makefile b/examples/posix_sockets/Makefile
index f2807361a9..32ca1e8bdf 100644
--- a/examples/posix_sockets/Makefile
+++ b/examples/posix_sockets/Makefile
@@ -15,6 +15,8 @@ USEMODULE += auto_init_gnrc_netif
 USEMODULE += gnrc_ipv6_default
 USEMODULE += sock_udp
 USEMODULE += posix_sockets
+USEMODULE += posix_select
+CFLAGS += -DPOSIX_SETSOCKOPT
 USEMODULE += posix_sleep
 USEMODULE += posix_inet
 # Add also the shell, some shell commands

and
$ BOARD=atmega256rfr2-xpro make

@benpicco
Copy link
Contributor

feel free to squash

@mrdeep1
Copy link
Contributor Author

mrdeep1 commented Sep 27, 2023

squashed and pushed.

@benpicco
Copy link
Contributor

bors merge

bors bot added a commit that referenced this pull request Sep 28, 2023
19946: posix_sockets.c: Fix 2 byte int compilation errors r=benpicco a=mrdeep1



19953: boards/esp32s3-wt32-sc01-plus: fix Kconfig r=aabadie a=gschorcht

### Contribution description

This PR fixes a remaining Kconfig mismatch. It should fix the last compilation problem of the nightly.

### Testing procedure

```
python3 dist/tools/compile_test/compile_like_murdock.py -a tests/drivers/ili9341/ -b esp32s3-wt32-sc01-plus
```
should fail w/o this PR but should succeed with this PR.

### Issues/PRs references


Co-authored-by: Jon Shallow <supjps-libcoap@jpshallow.com>
Co-authored-by: Gunar Schorcht <gunar@schorcht.net>
@bors
Copy link
Contributor

bors bot commented Sep 28, 2023

Build failed (retrying...):

bors bot added a commit that referenced this pull request Sep 28, 2023
19946: posix_sockets.c: Fix 2 byte int compilation errors r=benpicco a=mrdeep1



Co-authored-by: Jon Shallow <supjps-libcoap@jpshallow.com>
@bors
Copy link
Contributor

bors bot commented Sep 28, 2023

Build failed:

@benpicco
Copy link
Contributor

bors merge

@bors
Copy link
Contributor

bors bot commented Sep 29, 2023

Build succeeded!

The publicly hosted instance of bors-ng is deprecated and will go away soon.

If you want to self-host your own instance, instructions are here.
For more help, visit the forum.

If you want to switch to GitHub's built-in merge queue, visit their help page.

@bors bors bot merged commit 149cee4 into RIOT-OS:master Sep 29, 2023
26 checks passed
@mrdeep1 mrdeep1 deleted the posix_setsockopt branch September 29, 2023 13:32
@MrKevinWeiss MrKevinWeiss added this to the Release 2023.10 milestone Nov 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: sys Area: System 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.

None yet

4 participants