[libdvbpsi-devel] Fix dvbpsi_message() for cases where _GNU_SOURCE is not defined.

Jean-Paul Saman git at videolan.org
Mon Sep 8 14:49:47 CEST 2014


libdvbpsi | branch: master | Jean-Paul Saman <jpsaman at videolan.org> | Mon Sep  8 13:45:52 2014 +0200| [568fca6f813f8de89b1e700df34926a0f3af9de5] | committer: Jean-Paul Saman

Fix dvbpsi_message() for cases where _GNU_SOURCE is not defined.

If _GNU_SOURCE is not defined, then print the messages without leaking
memory.

Based on a patch from Guilherme Lima Bernal <lb-guilherme at live.com> and
Timothy Gu <timothygu99 at gmail.com>.

> http://git.videolan.org/gitweb.cgi/libdvbpsi.git/?a=commit;h=568fca6f813f8de89b1e700df34926a0f3af9de5
---

 src/dvbpsi.c |   27 ++++++++++++++-------------
 1 file changed, 14 insertions(+), 13 deletions(-)

diff --git a/src/dvbpsi.c b/src/dvbpsi.c
index 89d4932..4ae1cda 100644
--- a/src/dvbpsi.c
+++ b/src/dvbpsi.c
@@ -512,7 +512,7 @@ bool dvbpsi_packet_push(dvbpsi_t *p_dvbpsi, uint8_t* p_data)
 #   define DVBPSI_MSG_SIZE 1024
 #endif
 
-#define DVBPSI_MSG_FORMAT "libdvbpsi (%s): %s"
+#define DVBPSI_MSG_FORMAT "libdvbpsi (%s): "
 
 #ifdef HAVE_VARIADIC_MACROS
 void dvbpsi_message(dvbpsi_t *dvbpsi, const dvbpsi_msg_level_t level, const char *fmt, ...)
@@ -531,15 +531,10 @@ void dvbpsi_message(dvbpsi_t *dvbpsi, const dvbpsi_msg_level_t level, const char
             va_end(ap);
             return;
         }
-        if (snprintf(&msg, DVBPSI_MSG_SIZE, DVBPSI_MSG_FORMAT, ap) < 0) {
-            va_end(ap);
-            free(msg);
-            return;
-        }
-        int err = vsnprintf(&msg, DVBPSI_MSG_SIZE, fmt, ap);
+        int err = vsnprintf(msg, DVBPSI_MSG_SIZE, DVBPSI_MSG_FORMAT fmt, ap);
 #endif
         va_end(ap);
-        if (err > DVBPSI_MSG_NONE) {
+        if (err > 0) {
             if (dvbpsi->pf_message)
                 dvbpsi->pf_message(dvbpsi, level, msg);
         }
@@ -561,7 +556,7 @@ void dvbpsi_message(dvbpsi_t *dvbpsi, const dvbpsi_msg_level_t level, const char
             return;                                             \
         }                                                       \
         char *msg = NULL;                                       \
-        if (asprintf(&msg, DVBPSI_MSG_FORMAT, src, tmp) < 0) {  \
+        if (asprintf(&msg, DVBPSI_MSG_FORMAT "%s", src, tmp) < 0) { \
             va_end(ap);                                         \
             free(tmp);                                          \
             return;                                             \
@@ -575,25 +570,31 @@ void dvbpsi_message(dvbpsi_t *dvbpsi, const dvbpsi_msg_level_t level, const char
         free(msg);                                              \
     } while(0);
 #   else
-#       define DVBPSI_MSG_COMMON                                \
+#       define DVBPSI_MSG_COMMON(level)                         \
     do {                                                        \
         va_list ap;                                             \
         va_start(ap, fmt);                                      \
+        char *tmp = malloc(DVBPSI_MSG_SIZE);                    \
         char *msg = malloc(DVBPSI_MSG_SIZE);                    \
-        if (msg == NULL) {                                      \
+        if ((tmp == NULL) || (msg == NULL)) {                   \
             va_end(ap);                                         \
+            if (tmp) free(tmp);                                 \
+            if (msg) free(msg);                                 \
             return;                                             \
         }                                                       \
-        if (snprintf(&msg, DVBPSI_MSG_SIZE, DVBPSI_MSG_FORMAT, src) < 0) { \
+        if (vsnprintf(tmp, DVBPSI_MSG_SIZE, fmt, ap) < 0) {     \
             va_end(ap);                                         \
+            if (tmp) free(tmp);                                 \
+            if (msg) free(msg);                                 \
             return;                                             \
         }                                                       \
-        int err = vsnprintf(&msg, DVBPSI_MSG_SIZE, fmt, ap);    \
         va_end(ap);                                             \
+        int err = snprintf(msg, DVBPSI_MSG_SIZE, DVBPSI_MSG_FORMAT "%s", src, tmp); \
         if (err > 0) {                                          \
             if (dvbpsi->pf_message)                             \
                 dvbpsi->pf_message(dvbpsi, level, msg);         \
         }                                                       \
+        free(tmp);                                              \
         free(msg);                                              \
     } while(0);
 #   endif



More information about the libdvbpsi-devel mailing list