shithub: opus-tools

Download patch

ref: ebb4718a556bb9f339939838cf20278067ecede3
parent: a0b181ca844d919be7de26137ddedb0aec69b4b6
author: Mark Harris <mark.hsj@gmail.com>
date: Thu Feb 1 17:23:21 EST 2018

Use correct format specifier for 64-bit types

Correct fprintf format string warnings with libopus 1.3-beta on 64-bit
Linux (long long vs. long).  Also enable format string warnings for
oi_info(), oi_warn(), and oi_error() and correct those warnings as well.

--- a/configure.ac
+++ b/configure.ac
@@ -26,6 +26,7 @@
 AM_PROG_CC_C_O
 
 AC_PROG_CC_C99
+AC_CHECK_HEADERS([inttypes.h])
 AC_C_BIGENDIAN
 AC_C_CONST
 AC_C_INLINE
--- a/src/info_opus.c
+++ b/src/info_opus.c
@@ -214,7 +214,7 @@
         if(gp > 0) {
             if(gp < inf->lastgranulepos)
                 oi_warn(_("WARNING: granulepos in stream %d decreases from %"
-                        I64FORMAT " to %" I64FORMAT "\n" ),
+                        PRId64 " to %" PRId64 "\n" ),
                         stream->num, inf->lastgranulepos, gp);
             if(inf->lastgranulepos==0 && inf->firstgranule==-1) {
                 /*First timed page, now we can recover the start time.*/
@@ -226,21 +226,21 @@
                   else inf->firstgranule=0;
                 }
             }
-            if(inf->total_samples<gp-inf->firstgranule)oi_warn(_("WARNING: Sample count behind granule (%" I64FORMAT "<%" I64FORMAT ") in stream %d\n"),
-                (long long)inf->total_samples,(long long)(gp-inf->firstgranule),stream->num);
+            if(inf->total_samples<gp-inf->firstgranule)oi_warn(_("WARNING: Sample count behind granule (%" PRId64 "<%" PRId64 ") in stream %d\n"),
+                inf->total_samples,gp-inf->firstgranule,stream->num);
             if(!ogg_page_eos(page) && (inf->total_samples>gp-inf->firstgranule))
-                oi_warn(_("WARNING: Sample count ahead of granule (%" I64FORMAT ">%" I64FORMAT ") in stream %d\n"),
-                (long long)inf->total_samples,(long long)(gp-inf->firstgranule),stream->num);
+                oi_warn(_("WARNING: Sample count ahead of granule (%" PRId64 ">%" PRId64 ") in stream %d\n"),
+                inf->total_samples,gp-inf->firstgranule,stream->num);
             inf->lastlastgranulepos = inf->lastgranulepos;
             inf->lastgranulepos = gp;
             if(!packets)
-                oi_warn(_("WARNING: Page with positive granpos (%" I64FORMAT ") on a page with no completed packets in stream %d\n"),gp,stream->num);
+                oi_warn(_("WARNING: Page with positive granpos (%" PRId64 ") on a page with no completed packets in stream %d\n"),gp,stream->num);
         }
         else if(packets) {
             /* Only do this if we saw at least one packet ending on this page.
              * It's legal (though very unusual) to have no packets in a page at
              * all - this is occasionally used to have an empty EOS page */
-            oi_warn(_("Negative or zero granulepos (%" I64FORMAT ") on Opus stream outside of headers. This file was created by a buggy encoder\n"), gp);
+            oi_warn(_("Negative or zero granulepos (%" PRId64 ") on Opus stream outside of headers. This file was created by a buggy encoder\n"), gp);
         }
         inf->overhead_bytes += page->header_len;
         if(page_samples)inf->last_page_duration = page_samples;
@@ -270,7 +270,7 @@
         seconds = (long)(time - minutes*60);
         milliseconds = (long)((time - minutes*60 - seconds)*1000);
         if(inf->lastgranulepos-inf->firstgranule<inf->oh.preskip)
-           oi_error(_("\tERROR: stream %d has a negative duration: %" I64FORMAT "-%" I64FORMAT "-%d=%" I64FORMAT "\n"),stream->num,
+           oi_error(_("\tERROR: stream %d has a negative duration: %" PRId64 "-%" PRId64 "-%d=%" PRId64 "\n"),stream->num,
            inf->lastgranulepos,inf->firstgranule,inf->oh.preskip,inf->lastgranulepos-inf->firstgranule-inf->oh.preskip);
         if((inf->total_samples-inf->last_page_duration)>(inf->lastgranulepos-inf->firstgranule))
            oi_error(_("\tERROR: stream %d has interior holes or more than one page of end trimming\n"),stream->num);
@@ -291,7 +291,7 @@
             inf->max_packet_duration/48.,inf->total_samples/(double)inf->total_packets/48.,inf->min_packet_duration/48.);
         if(inf->total_pages)oi_info(_("\tPage duration: %8.1fms (max), %6.1fms (avg), %6.1fms (min)\n"),
             inf->max_page_duration/48.,inf->total_samples/(double)inf->total_pages/48.,inf->min_page_duration/48.);
-        oi_info(_("\tTotal data length: %" I64FORMAT " bytes (overhead: %0.3g%%)\n"),inf->bytes,(double)inf->overhead_bytes/inf->bytes*100.);
+        oi_info(_("\tTotal data length: %" PRId64 " bytes (overhead: %0.3g%%)\n"),inf->bytes,(double)inf->overhead_bytes/inf->bytes*100.);
         oi_info(_("\tPlayback length: %ldm:%02ld.%03lds\n"), minutes, seconds, milliseconds);
         oi_info(_("\tAverage bitrate: %0.4g kb/s, w/o overhead: %.04g kb/s%s\n"),time<=0?0:inf->bytes*8/time/1000.0,
             time<=0?0:(inf->bytes-inf->overhead_bytes)*8/time/1000.0,
--- a/src/opusenc.c
+++ b/src/opusenc.c
@@ -42,6 +42,10 @@
 #endif
 #include <math.h>
 
+#ifdef HAVE_INTTYPES_H
+# include <inttypes.h>
+#endif
+
 #ifdef _MSC_VER
 # define snprintf _snprintf
 #endif
@@ -51,9 +55,7 @@
 /* We need the following two to set stdout to binary */
 # include <io.h>
 # include <fcntl.h>
-# define I64FORMAT "I64d"
 #else
-# define I64FORMAT "lld"
 # define fopen_utf8(_x,_y) fopen((_x),(_y))
 # define argc_utf8 argc
 # define argv_utf8 argv
@@ -68,6 +70,15 @@
 #include "diag_range.h"
 #include "cpusupport.h"
 #include <opusenc.h>
+
+/* printf format specifier for opus_int64 */
+#if !defined opus_int64 && defined PRId64
+# define I64FORMAT PRId64
+#elif defined WIN32 || defined _WIN32
+# define I64FORMAT "I64d"
+#else
+# define I64FORMAT "lld"
+#endif
 
 #ifdef VALGRIND
 # include <valgrind/memcheck.h>
--- a/src/opusinfo.c
+++ b/src/opusinfo.c
@@ -585,7 +585,7 @@
            SPECIFICATION argument to opusenc/flac/etc. (except without an image
            filename, since we don't know the original).*/
          oi_info("\t%.*s%u|%.*s|%.*s|%ux%ux%u",
-               sep+1-comment, comment, (unsigned)picture_type,
+               (int)(sep+1-comment), comment, (unsigned)picture_type,
                mime_type_length, data+8,
                description_length, data+12+mime_type_length,
                (unsigned)width, (unsigned)height, (unsigned)depth);
@@ -807,7 +807,7 @@
     while((ret = ogg_sync_pageseek(ogsync, page)) <= 0) {
         if(ret < 0) {
             /* unsynced, we jump over bytes to a possible capture - we don't need to read more just yet */
-            oi_warn(_("WARNING: Hole in data (%d bytes) found at approximate offset %" I64FORMAT " bytes. Corrupted Ogg.\n"), -ret, *written);
+            oi_warn(_("WARNING: Hole in data (%d bytes) found at approximate offset %" PRId64 " bytes. Corrupted Ogg.\n"), -ret, *written);
             continue;
         }
 
--- a/src/opusinfo.h
+++ b/src/opusinfo.h
@@ -9,11 +9,16 @@
 /*No NLS support for now*/
 #define _(X) (X)
 
-#ifdef _WIN32
-#define I64FORMAT "I64d"
-#else
-#define I64FORMAT "lld"
+#ifdef HAVE_INTTYPES_H
+# include <inttypes.h>
 #endif
+#ifndef PRId64
+# if defined WIN32 || defined _WIN32
+#  define PRId64 "I64d"
+# else
+#  define PRId64 "lld"
+# endif
+#endif
 
 typedef struct _stream_processor {
     void (*process_page)(struct _stream_processor *, ogg_page *);
@@ -45,8 +50,13 @@
     int in_headers;
 } stream_set;
 
+#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 3)
+# define OI_FORMAT_PRINTF __attribute__((__format__(printf, 1, 2)))
+#else
+# define OI_FORMAT_PRINTF
+#endif
 
-void oi_info(char *format, ...);
-void oi_warn(char *format, ...);
-void oi_error(char *format, ...);
+void oi_info(char *format, ...) OI_FORMAT_PRINTF;
+void oi_warn(char *format, ...) OI_FORMAT_PRINTF;
+void oi_error(char *format, ...) OI_FORMAT_PRINTF;
 void check_xiph_comment(stream_processor *stream, int i, const char *comment, int comment_length);