diff mbox series

[FFmpeg-devel,v3,2/3] avutil/error: Provide better feedback about unknown error codes

Message ID 20240718104757.2809396-3-ffmpeg-devel@pileofstuff.org
State New
Headers show
Series Protect against undocumented error codes | expand

Checks

Context Check Description
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

Andrew Sayers July 18, 2024, 10:46 a.m. UTC
AVERROR messages should always be less than zero, and are often FourCCs.

For error codes that aren't explicitly handled by error.c (e.g. undocumented
system error codes, or internal error codes that leaked to the public API),
print the FourCC code so the user has a little more information to work with.

If a non-negative number somehow gets passed to this function,
print a message saying this shouldn't happen.
---
 libavutil/error.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/libavutil/error.c b/libavutil/error.c
index 90bab7b9d3..0f748bd9e5 100644
--- a/libavutil/error.c
+++ b/libavutil/error.c
@@ -20,6 +20,7 @@ 
 #define _XOPEN_SOURCE 600 /* XSI-compliant version of strerror_r */
 #include <stdio.h>
 #include <string.h>
+#include "avutil.h"
 #include "config.h"
 #include "avstring.h"
 #include "error.h"
@@ -119,6 +120,8 @@  int av_strerror(int errnum, char *errbuf, size_t errbuf_size)
     }
     if (entry) {
         av_strlcpy(errbuf, entry->str, errbuf_size);
+    } else if (errnum >= 0) {
+        snprintf(errbuf, errbuf_size, "Impossible: non-negative error number %d occurred, please report this bug", errnum);
     } else {
 #if HAVE_STRERROR_R
         ret = AVERROR(strerror_r(AVUNERROR(errnum), errbuf, errbuf_size));
@@ -126,7 +129,7 @@  int av_strerror(int errnum, char *errbuf, size_t errbuf_size)
         ret = -1;
 #endif
         if (ret < 0)
-            snprintf(errbuf, errbuf_size, "Error number %d occurred", errnum);
+            snprintf(errbuf, errbuf_size, "Error number -0x%X (%s) occurred, please report this bug", -errnum, av_fourcc2str(-errnum));
     }
 
     return ret;