Procházet zdrojové kódy

libdpkg: Return error in error_context_errmsg_format() only if truncated

In case we have to use the emergency buffer because the previous
vasprintf() call failed, we should only return an error code if the
vsnprintf() call on the emergency buffer truncates the output.
Guillem Jover před 10 roky
rodič
revize
d1f570622c
2 změnil soubory, kde provedl 9 přidání a 1 odebrání
  1. 4 0
      debian/changelog
  2. 5 1
      lib/dpkg/ehandle.c

+ 4 - 0
debian/changelog

@@ -34,6 +34,10 @@ dpkg (1.18.11) UNRELEASED; urgency=medium
     Reported by Helmut Grohne <helmut@subdivi.de>.
     Reported by Helmut Grohne <helmut@subdivi.de>.
   * Only set the error context message in libdpkg if it has been formatted
   * Only set the error context message in libdpkg if it has been formatted
     correctly.
     correctly.
+  * Return error in error_context_errmsg_format() only if the error message
+    gets truncated. In case we have to use the emergency buffer because the
+    previous vasprintf() call failed, we should only return an error code if
+    the vsnprintf() call on the emergency buffer truncates the output.
   * Architecture support:
   * Architecture support:
     - Add support for AIX operating system.
     - Add support for AIX operating system.
   * Portability:
   * Portability:

+ 5 - 1
lib/dpkg/ehandle.c

@@ -202,7 +202,11 @@ error_context_errmsg_format(const char *fmt, va_list args)
     /* If there was any error, just use the emergency error message buffer,
     /* If there was any error, just use the emergency error message buffer,
      * even if it ends up being truncated, at least we will have a big part
      * even if it ends up being truncated, at least we will have a big part
      * of the problem. */
      * of the problem. */
-    vsnprintf(emergency.errmsg, sizeof(emergency.errmsg), fmt, args);
+    rc = vsnprintf(emergency.errmsg, sizeof(emergency.errmsg), fmt, args);
+
+    /* Return failure only if we get truncated. */
+    if (rc >= (int)sizeof(emergency.errmsg))
+      rc = -1;
 
 
     error_context_errmsg_set(econtext, emergency.errmsg);
     error_context_errmsg_set(econtext, emergency.errmsg);
   }
   }