Commit 568fca6f authored by Jean-Paul Saman's avatar 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@live.com> and
Timothy Gu <timothygu99@gmail.com>.
parent e0104451
...@@ -512,7 +512,7 @@ bool dvbpsi_packet_push(dvbpsi_t *p_dvbpsi, uint8_t* p_data) ...@@ -512,7 +512,7 @@ bool dvbpsi_packet_push(dvbpsi_t *p_dvbpsi, uint8_t* p_data)
# define DVBPSI_MSG_SIZE 1024 # define DVBPSI_MSG_SIZE 1024
#endif #endif
#define DVBPSI_MSG_FORMAT "libdvbpsi (%s): %s" #define DVBPSI_MSG_FORMAT "libdvbpsi (%s): "
#ifdef HAVE_VARIADIC_MACROS #ifdef HAVE_VARIADIC_MACROS
void dvbpsi_message(dvbpsi_t *dvbpsi, const dvbpsi_msg_level_t level, const char *fmt, ...) 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 ...@@ -531,15 +531,10 @@ void dvbpsi_message(dvbpsi_t *dvbpsi, const dvbpsi_msg_level_t level, const char
va_end(ap); va_end(ap);
return; return;
} }
if (snprintf(&msg, DVBPSI_MSG_SIZE, DVBPSI_MSG_FORMAT, ap) < 0) { int err = vsnprintf(msg, DVBPSI_MSG_SIZE, DVBPSI_MSG_FORMAT fmt, ap);
va_end(ap);
free(msg);
return;
}
int err = vsnprintf(&msg, DVBPSI_MSG_SIZE, fmt, ap);
#endif #endif
va_end(ap); va_end(ap);
if (err > DVBPSI_MSG_NONE) { if (err > 0) {
if (dvbpsi->pf_message) if (dvbpsi->pf_message)
dvbpsi->pf_message(dvbpsi, level, msg); dvbpsi->pf_message(dvbpsi, level, msg);
} }
...@@ -561,7 +556,7 @@ void dvbpsi_message(dvbpsi_t *dvbpsi, const dvbpsi_msg_level_t level, const char ...@@ -561,7 +556,7 @@ void dvbpsi_message(dvbpsi_t *dvbpsi, const dvbpsi_msg_level_t level, const char
return; \ return; \
} \ } \
char *msg = NULL; \ 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); \ va_end(ap); \
free(tmp); \ free(tmp); \
return; \ return; \
...@@ -575,25 +570,31 @@ void dvbpsi_message(dvbpsi_t *dvbpsi, const dvbpsi_msg_level_t level, const char ...@@ -575,25 +570,31 @@ void dvbpsi_message(dvbpsi_t *dvbpsi, const dvbpsi_msg_level_t level, const char
free(msg); \ free(msg); \
} while(0); } while(0);
# else # else
# define DVBPSI_MSG_COMMON \ # define DVBPSI_MSG_COMMON(level) \
do { \ do { \
va_list ap; \ va_list ap; \
va_start(ap, fmt); \ va_start(ap, fmt); \
char *tmp = malloc(DVBPSI_MSG_SIZE); \
char *msg = malloc(DVBPSI_MSG_SIZE); \ char *msg = malloc(DVBPSI_MSG_SIZE); \
if (msg == NULL) { \ if ((tmp == NULL) || (msg == NULL)) { \
va_end(ap); \ va_end(ap); \
if (tmp) free(tmp); \
if (msg) free(msg); \
return; \ return; \
} \ } \
if (snprintf(&msg, DVBPSI_MSG_SIZE, DVBPSI_MSG_FORMAT, src) < 0) { \ if (vsnprintf(tmp, DVBPSI_MSG_SIZE, fmt, ap) < 0) { \
va_end(ap); \ va_end(ap); \
if (tmp) free(tmp); \
if (msg) free(msg); \
return; \ return; \
} \ } \
int err = vsnprintf(&msg, DVBPSI_MSG_SIZE, fmt, ap); \
va_end(ap); \ va_end(ap); \
int err = snprintf(msg, DVBPSI_MSG_SIZE, DVBPSI_MSG_FORMAT "%s", src, tmp); \
if (err > 0) { \ if (err > 0) { \
if (dvbpsi->pf_message) \ if (dvbpsi->pf_message) \
dvbpsi->pf_message(dvbpsi, level, msg); \ dvbpsi->pf_message(dvbpsi, level, msg); \
} \ } \
free(tmp); \
free(msg); \ free(msg); \
} while(0); } while(0);
# endif # endif
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment