[libbluray-devel] logging: check for *sprintf errors

hpi1 git at videolan.org
Sun Nov 8 22:04:40 CET 2015


libbluray | branch: master | hpi1 <hpi1 at anonymous.org> | Thu Nov  5 17:30:40 2015 +0200| [d359fb4e58123e6d5420bd3b59a83a98ed13a782] | committer: hpi1

logging: check for *sprintf errors

> http://git.videolan.org/gitweb.cgi/libbluray.git/?a=commit;h=d359fb4e58123e6d5420bd3b59a83a98ed13a782
---

 src/util/logging.c |   23 +++++++++++++++++++----
 1 file changed, 19 insertions(+), 4 deletions(-)

diff --git a/src/util/logging.c b/src/util/logging.c
index b8ef1f5..76ae7a4 100644
--- a/src/util/logging.c
+++ b/src/util/logging.c
@@ -81,19 +81,34 @@ void bd_debug(const char *file, int line, uint32_t mask, const char *format, ...
 
     if (mask & debug_mask) {
         const char *f = strrchr(file, DIR_SEP_CHAR);
-        char buffer[4096], *pt = buffer;
+        char buffer[4096];
         va_list args;
+        int len, len2;
 
-        pt += sprintf(buffer, "%s:%d: ", f ? f + 1 : file, line);
+        len = sprintf(buffer, "%s:%d: ", f ? f + 1 : file, line);
+        if (len < 0) {
+            return;
+        }
 
         va_start(args, format);
-        vsnprintf(pt, sizeof(buffer) - (size_t)(intptr_t)(pt - buffer) - 1, format, args);
+        len2 = vsnprintf(buffer + len, sizeof(buffer) - len - 1, format, args);
         va_end(args);
 
+        if (len2 < 0) {
+            return;
+        }
+
         if (log_func) {
+            buffer[sizeof(buffer)-1] = 0;
             log_func(buffer);
+
         } else {
-            fprintf(logfile, "%s", buffer);
+            len += len2;
+            if (len >= sizeof(buffer)) {
+                len = sizeof(buffer);
+            }
+
+            fwrite(buffer, len, 1, logfile);
         }
     }
 }



More information about the libbluray-devel mailing list