Skip to content

Commit

Permalink
netconsd: Fix printing of uint64_t's in modules
Browse files Browse the repository at this point in the history
Summary:
Building on arm64 currently warns with
```
logger.cc: In function 'void write_log(logtarget&, msg_buf*, ncrx_msg*)':
logger.cc:121:19: warning: format '%lu' expects argument of type 'long unsigned int', but argument 3 has type 'uint64_t' {aka 'long long unsigned int'} [-Wformat=]
   dprintf(tgt.fd, "%06lu %014lu %d %d %s%s%s%s%s\n", msg->seq,
                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  ~~~~~~~~
logger.cc:121:19: warning: format '%lu' expects argument of type 'long unsigned int', but argument 4 has type 'uint64_t' {aka 'long long unsigned int'} [-Wformat=]
g++ logger.o -lpthread -lrt -ldl -shared -static-libstdc++ -static-libgcc \
		-o logger.so
```
It also segfaults as soon as it gets an extended message, due to the %lu trying to
read 8 bytes, while the argument is just 4.  Use a portable type to make the build
dtrt on x86-64 and arm64

Reviewed By: davide125

Differential Revision: D30970654

fbshipit-source-id: 6edc47d48486e2f66644bbec6405024d8e948cad
  • Loading branch information
kernelslacker authored and facebook-github-bot committed Oct 7, 2021
1 parent c0900ca commit 8ad824b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
6 changes: 4 additions & 2 deletions modules/logger.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <cstring>
#include <functional>
#include <unordered_map>
#include <inttypes.h>

#include <fcntl.h>
#include <netdb.h>
Expand Down Expand Up @@ -118,8 +119,9 @@ static void write_log(struct logtarget& tgt, struct msg_buf *buf,
if (!msg)
dprintf(tgt.fd, "%s\n", buf->buf);
else
dprintf(tgt.fd, "%06lu %014lu %d %d %s%s%s%s%s\n", msg->seq,
msg->ts_usec, msg->facility, msg->level,
dprintf(tgt.fd, "%06" PRIu64 " %014" PRIu64 " %d %d %s%s%s%s%s\n",
msg->seq, msg->ts_usec,
msg->facility, msg->level,
msg->cont_start ? "[CONT START] " : "",
msg->cont ? "[CONT] " : "",
msg->oos ? "[OOS] ": "",
Expand Down
3 changes: 2 additions & 1 deletion modules/printer.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <stdlib.h>
#include <stdio.h>
#include <arpa/inet.h>
#include <inttypes.h>

#include <msgbuf-struct.h>
#include <ncrx-struct.h>
Expand Down Expand Up @@ -37,7 +38,7 @@ void netconsd_output_handler(int t, struct in6_addr *src, struct msg_buf *buf,
if (!msg)
printf("%40s: %s\n", addr, buf->buf);
else
printf("%40s: S%06lu T%014lu F%d/L%d %s%s%s%s%s\n", addr,
printf("%40s: S%06" PRIu64 " T%014" PRIu64 " F%d/L%d %s%s%s%s%s\n", addr,
msg->seq, msg->ts_usec, msg->facility, msg->level,
msg->cont_start ? "[CONT START] " : "",
msg->cont ? "[CONT] " : "",
Expand Down

0 comments on commit 8ad824b

Please sign in to comment.