diff mbox

[LEDE-DEV] libubox: Add printf attribute to several functions.

Message ID 1477985559-10166-1-git-send-email-rosenp@gmail.com
State Changes Requested
Headers show

Commit Message

Rosen Penev Nov. 1, 2016, 7:32 a.m. UTC
Also fixed some formats. Without this attribute, GCC does not see the several functions as variants of the -f family of functions which prevents it from warning about their usage. Might help with optimization too. I avoided defining the attributes in utils.h since different functions have different requirements.

Signed-off by: Rosen Penev <rosenp@gmail.com>
---
 blobmsg.c | 5 +++--
 ulog.c    | 6 +++++-
 usock.c   | 2 +-
 ustream.c | 2 ++
 4 files changed, 11 insertions(+), 4 deletions(-)

Comments

Felix Fietkau Nov. 1, 2016, 12:35 p.m. UTC | #1
On 2016-11-01 08:32, Rosen Penev wrote:
> Also fixed some formats. Without this attribute, GCC does not see the several functions as variants of the -f family of functions which prevents it from warning about their usage. Might help with optimization too. I avoided defining the attributes in utils.h since different functions have different requirements.
> 
> Signed-off by: Rosen Penev <rosenp@gmail.com>
I think putting these annotations in the .c files does not make any
sense, since external code calling these functions will not see it.
Also, it's probably a good idea to define __printf() and make it no-op
in case GCC is not used.

Another thing: Please don't fix different unrelated things in the same
patch, send multiple patches instead.

- Felix
diff mbox

Patch

diff --git a/blobmsg.c b/blobmsg.c
index 1e93376..73f927e 100644
--- a/blobmsg.c
+++ b/blobmsg.c
@@ -227,7 +227,7 @@  blobmsg_open_nested(struct blob_buf *buf, const char *name, bool array)
 	return (void *)offset;
 }
 
-int
+__attribute__((format(printf, 3, 0))) int
 blobmsg_vprintf(struct blob_buf *buf, const char *name, const char *format, va_list arg)
 {
 	va_list arg2;
@@ -248,7 +248,8 @@  blobmsg_vprintf(struct blob_buf *buf, const char *name, const char *format, va_l
 	return ret;
 }
 
-int
+
+__attribute__((format(printf, 3, 4))) int
 blobmsg_printf(struct blob_buf *buf, const char *name, const char *format, ...)
 {
 	va_list ap;
diff --git a/ulog.c b/ulog.c
index 296605d..7a862b3 100644
--- a/ulog.c
+++ b/ulog.c
@@ -86,12 +86,13 @@  static void ulog_defaults(void)
 	_ulog_initialized = 1;
 }
 
+__attribute__((format(printf, 2, 0)))
 static void ulog_kmsg(int priority, const char *fmt, va_list ap)
 {
 	FILE *kmsg;
 
 	if ((kmsg = fopen("/dev/kmsg", "r+")) != NULL) {
-		fprintf(kmsg, "<%u>", priority);
+		fprintf(kmsg, "<%d>", priority);
 
 		if (_ulog_ident)
 			fprintf(kmsg, "%s: ", _ulog_ident);
@@ -101,6 +102,7 @@  static void ulog_kmsg(int priority, const char *fmt, va_list ap)
 	}
 }
 
+__attribute__((format(printf, 2, 0)))
 static void ulog_stdio(int priority, const char *fmt, va_list ap)
 {
 	FILE *out = stderr;
@@ -111,6 +113,7 @@  static void ulog_stdio(int priority, const char *fmt, va_list ap)
 	vfprintf(out, fmt, ap);
 }
 
+__attribute__((format(printf, 2, 0)))
 static void ulog_syslog(int priority, const char *fmt, va_list ap)
 {
 	vsyslog(priority, fmt, ap);
@@ -141,6 +144,7 @@  void ulog_threshold(int threshold)
 	_ulog_threshold = threshold;
 }
 
+__attribute__((format(printf, 2, 3)))
 void ulog(int priority, const char *fmt, ...)
 {
 	va_list ap;
diff --git a/usock.c b/usock.c
index 0ce5390..0983806 100644
--- a/usock.c
+++ b/usock.c
@@ -247,7 +247,7 @@  const char *usock_port(int port)
 	if (port < 0 || port > 65535)
 		return NULL;
 
-	snprintf(buffer, sizeof(buffer), "%u", port);
+	snprintf(buffer, sizeof(buffer), "%d", port);
 
 	return buffer;
 }
diff --git a/ustream.c b/ustream.c
index d36ce08..2570577 100644
--- a/ustream.c
+++ b/ustream.c
@@ -464,6 +464,7 @@  int ustream_write(struct ustream *s, const char *data, int len, bool more)
 
 #define MAX_STACK_BUFLEN	256
 
+__attribute__((format(printf, 2, 0)))
 int ustream_vprintf(struct ustream *s, const char *format, va_list arg)
 {
 	struct ustream_buf_list *l = &s->w;
@@ -531,6 +532,7 @@  int ustream_vprintf(struct ustream *s, const char *format, va_list arg)
 	return wr;
 }
 
+__attribute__((format(printf, 2, 3)))
 int ustream_printf(struct ustream *s, const char *format, ...)
 {
 	va_list arg;