signalwire / libstirshaken

C library implementing STIR-shaken STI-SP AS/VS, STI-CA
MIT License
31 stars 23 forks source link

Fix size_t and time_t format specifiers #114

Open micmac1 opened 3 years ago

micmac1 commented 3 years ago

Use "%zu" for size_t and cast time_t to (long long) before printing with "%lld". Fixes the below mentioned warnings.

time_t:

src/stir_shaken_passport.c:818:98: error: format '%zu' expects argument of type 'size_t', but argument 4 has type 'time_t' {aka 'long long int'} [-Werror=format=]
  818 |                 snprintf(err_buf, STIR_SHAKEN_ERROR_BUF_LEN, "PASSporT's @iat (in seconds) is: %zu, freshness is: %u, BUT now is %zu (this is PASSporT to the future, too young, not valid yet)", iat, iat_freshness, now_s);
      |                                                                                                ~~^                                                                                                ~~~
      |                                                                                                  |                                                                                                |
      |                                                                                                  unsigned int                                                                                     time_t {aka long long int}
      |                                                                                                %llu

size_t:

util/src/stir_shaken_ca.c:128:84: warning: format '%lu' expects argument of type 'long unsigned int', but argument 5 has type 'size_t' {aka 'unsigned int'} [-Wformat=]
  128 |                                 mg_printf(nc, "HTTP/1.1 %s %s\r\nContent-Length: %lu\r\nContent-Type: application/json\r\n\r\n%s\r\n\r\n", STIR_SHAKEN_HTTP_REQ_404_NOT_FOUND, error_phrase, strlen(error_body), error_body);
      |                                                                                  ~~^                                                                                                         ~~~~~~~~~~~~~~~~~~
      |                                                                                    |                                                                                                         |
      |                                                                                    long unsigned int                                                                                         size_t {aka unsigned int}
      |                                                                                  %u

Signed-off-by: Sebastian Kemper sebastian_ml@gmx.net

mjerris commented 3 years ago

These format specifiers actually differ by platform. what os/arch are you targeting where you have issues here?

micmac1 commented 3 years ago

These format specifiers actually differ by platform. what os/arch are you targeting where you have issues here?

Hi there, Linux/OpenWrt, lots of targets (ppc, mips, mipsel, mips64, aarch64, arc...)

Regarding time_t see also https://github.com/signalwire/freeswitch/pull/1409 (where @crienzo asked you to review a while ago).

Regarding size_t the common answer on stackoveflow as to what format specifier to use seems to be use "z" when you're dealing with compilers supporting C99. You already use "z" elsewhere in the lib, so I figure it's fine.

mrtrev commented 3 years ago

Looks like this is probably the resolution to the issue I just created #117

Any reason it hasn't been committed yet? I was just preparing to create a PR with the same change from %lu to %zu for all of the strlen calls.

andywolk commented 2 years ago

Shouldn't we have %zu in all the places without a cast?

micmac1 commented 2 years ago

Hi Andrey,

Not sure what you mean. Can you be specific and provide an example?

Kind regards, Seb

mrtrev commented 1 year ago

Voting again that this be merged. Doesn't seem like anyone has had an objection in a year now.

andywolk commented 1 year ago

Why do you cast variables?

micmac1 commented 1 year ago

Am 20. Dezember 2022 16:14:47 UTC schrieb Andrey Volk @.***>:

Why do you cast variables?

Because of Y2K38

https://en.m.wikipedia.org/wiki/Year_2038_problem

andywolk commented 1 year ago

Is there a way to fix the issue without casting?