libopkg: fix double-free crash on recursive package removal
[oweals/opkg-lede.git] / libopkg / opkg_message.c
index 81afc97eaace49998de2895d0156da26e67c1f2f..2862ea6933b7f02c9959cd08e739b92ef58aceb2 100644 (file)
@@ -14,8 +14,8 @@
    General Public License for more details.
 */
 
+#include <stdio.h>
 
-#include "includes.h"
 #include "opkg_conf.h"
 #include "opkg_message.h"
 #include "libbb/libbb.h"
@@ -64,10 +64,10 @@ print_error_list(void)
        struct errlist *err = error_list_head;
 
        if (err) {
-               printf("Collected errors:\n");
+               fprintf(stderr, "Collected errors:\n");
                /* Here we print the errors collected and free the list */
                while (err != NULL) {
-                       printf(" * %s", err->errmsg);
+                       fprintf(stderr, " * %s", err->errmsg);
                        err = err->next;
                }
        }
@@ -92,15 +92,30 @@ opkg_message (message_level_t level, const char *fmt, ...)
        va_start (ap, fmt);
 
        if (level == ERROR) {
-#define MSG_LEN 256
+#define MSG_LEN 4096
                char msg[MSG_LEN];
-               if (vsnprintf(msg, MSG_LEN, fmt, ap) >= MSG_LEN) {
-                       fprintf(stderr, "%s: Message truncated!\n",
+               int ret;
+               ret = vsnprintf(msg, MSG_LEN, fmt, ap);
+               if (ret < 0) {
+                       fprintf(stderr, "%s: encountered an output or encoding"
+                                       " error during vsnprintf.\n",
+                                       __FUNCTION__);
+                       va_end (ap);
+                       exit(EXIT_FAILURE);
+               }
+               if (ret >= MSG_LEN) {
+                       fprintf(stderr, "%s: Message truncated.\n",
                                        __FUNCTION__);
                }
                push_error_list(msg);
-       } else
-               vprintf(fmt, ap);
+       } else {
+               if (vprintf(fmt, ap) < 0) {
+                       fprintf(stderr, "%s: encountered an output or encoding"
+                                       " error during vprintf.\n",
+                                       __FUNCTION__);
+                       exit(EXIT_FAILURE);
+               }
+       }
 
        va_end (ap);
 }