From dfefb45edf3c4e7ccf8c2a47a3b765128f7c20c4 Mon Sep 17 00:00:00 2001
From: Alexander Dergachev <cy6erbr4in@gmail.com>
Date: Thu, 10 Apr 2008 22:31:33 +0400
Subject: [PATCH 1/4] aprintf is calling memfatal function now instead of
 calling abort function for consistency with xmalloc/xrealloc

---
 src/utils.c   |  2 +-
 src/xmalloc.c | 22 ++++++++++++++++++----
 src/xmalloc.h |  7 +++++++
 3 files changed, 26 insertions(+), 5 deletions(-)

diff --git a/src/utils.c b/src/utils.c
index 8c56d4db..3fd1435e 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -171,7 +171,7 @@ aprintf (const char *fmt, ...)
   ret = vasprintf (&str, fmt, args);
   va_end (args);
   if (ret < 0 && errno == ENOMEM)
-    abort ();                   /* for consistency with xmalloc/xrealloc */
+    memfatal ("aprintf", UNKNOWN_ATTEMPTED_SIZE);  /* for consistency with xmalloc/xrealloc */
   else if (ret < 0)
     return NULL;
   return str;
diff --git a/src/xmalloc.c b/src/xmalloc.c
index 75ecf5ef..aaf743df 100644
--- a/src/xmalloc.c
+++ b/src/xmalloc.c
@@ -53,15 +53,29 @@ as that of the covered work.  */
 /* Croak the fatal memory error and bail out with non-zero exit
    status.  */
 
-static void
+void
 memfatal (const char *context, long attempted_size)
 {
   /* Make sure we don't try to store part of the log line, and thus
      call malloc.  */
   log_set_save_context (false);
-  logprintf (LOG_ALWAYS,
-             _("%s: %s: Failed to allocate %ld bytes; memory exhausted.\n"),
-             exec_name, context, attempted_size);
+
+  /* We have different log outputs in different situations:
+     1) output without bytes information
+     2) output with bytes information  */
+  if (attempted_size == UNKNOWN_ATTEMPTED_SIZE)
+    {
+      logprintf (LOG_ALWAYS,
+                 _("%s: %s: Failed to allocate enough memory; memory exhausted.\n"),
+                 exec_name, context);
+    }
+  else
+    {
+      logprintf (LOG_ALWAYS,
+                 _("%s: %s: Failed to allocate %ld bytes; memory exhausted.\n"),
+                 exec_name, context, attempted_size);
+    }
+
   exit (1);
 }
 
diff --git a/src/xmalloc.h b/src/xmalloc.h
index 67b97e6e..ce326b1b 100644
--- a/src/xmalloc.h
+++ b/src/xmalloc.h
@@ -31,6 +31,13 @@ as that of the covered work.  */
 #ifndef XMALLOC_H
 #define XMALLOC_H
 
+/* Croak the fatal memory error and bail out with non-zero exit
+   status.  */
+void memfatal (const char *context, long attempted_size);
+
+/* Constant is using when we don`t know attempted size exactly */
+#define UNKNOWN_ATTEMPTED_SIZE -3
+
 /* Define this to use Wget's builtin malloc debugging, which is crude
    but occasionally useful.  It will make Wget a lot slower and
    larger, and susceptible to aborting if malloc_table overflows, so

From 311f76645f7637d295f2152b90fe8b3d5495706b Mon Sep 17 00:00:00 2001
From: Alexander Dergachev <cy6erbr4in@gmail.com>
Date: Fri, 11 Apr 2008 23:52:57 +0400
Subject: [PATCH 2/4] * utils.c (aprintf): Now we are setting limits (1 Mb) for
 text buffer when we use non-C99 vsnprintf.

---
 src/ChangeLog |  5 +++++
 src/utils.c   | 17 ++++++++++++++++-
 2 files changed, 21 insertions(+), 1 deletion(-)

diff --git a/src/ChangeLog b/src/ChangeLog
index fddcefd4..d320f150 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,8 @@
+2008-04-11  Alexander Dergachev  <cy6erbr4in@gmail.com>
+
+	* utils.c (aprintf): Now we are setting limits (1 Mb) for text
+	buffer when we use non-C99 vsnprintf.
+	
 2008-04-10  Alexander Dergachev  <cy6erbr4in@gmail.com>
 
 	* xmalloc.c, xmalloc.h (memfatal): Now exported; accepts an
diff --git a/src/utils.c b/src/utils.c
index 3fd1435e..3c873d0e 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -159,6 +159,13 @@ sepstring (const char *s)
    vsnprintf until the correct size is found.  Since Wget also ships a
    fallback implementation of vsnprintf, this should be portable.  */
 
+/* Constant is using for limits memory allocation for text buffer.
+   Applicable in situation when: vasprintf is not available in the system 
+   and vsnprintf return -1 when long line is truncated (in old versions of
+   glibc and in other system where C99 doesn`t support) */
+
+#define FMT_MAX_LENGTH 1048576
+
 char *
 aprintf (const char *fmt, ...)
 {
@@ -202,7 +209,15 @@ aprintf (const char *fmt, ...)
       if (n > -1)               /* C99 */
         size = n + 1;           /* precisely what is needed */
       else
-        size <<= 1;             /* twice the old size */
+        {
+          if (size >= FMT_MAX_LENGTH)  /* We have a huge buffer, */
+            {                          /* maybe we have some wrong format string? */
+              free (str);              /* In this case we must free already allocated memory, */
+              return NULL;             /* and return NULL pointer... */
+            }
+                                       /* else, we continue to grow our buffer. */
+          size <<= 1;                  /* twice the old size */
+        }
       str = xrealloc (str, size);
     }
 #endif /* not HAVE_VASPRINTF */

From 0e7917cda7c06b223745fb9ff95b2f129eee55de Mon Sep 17 00:00:00 2001
From: Alexander Dergachev <cy6erbr4in@gmail.com>
Date: Sat, 12 Apr 2008 19:13:23 +0400
Subject: [PATCH 3/4] * utils.c (aprintf): print log message and abort if we
 have too big text buffer.

---
 src/utils.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/src/utils.c b/src/utils.c
index 3c873d0e..af621402 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -213,7 +213,11 @@ aprintf (const char *fmt, ...)
           if (size >= FMT_MAX_LENGTH)  /* We have a huge buffer, */
             {                          /* maybe we have some wrong format string? */
               free (str);              /* In this case we must free already allocated memory, */
-              return NULL;             /* and return NULL pointer... */
+              logprintf (LOG_ALWAYS, 
+                         _("%s: aprintf: text buffer is too big (%ld bytes), \
+free already allocated memory and abort.\n"),
+                         exec_name, size);  /* printout a log message */
+              abort ();             /* and abort... */
             }
                                        /* else, we continue to grow our buffer. */
           size <<= 1;                  /* twice the old size */

From 667f4578a570699b69b16defa6f22e26a315c891 Mon Sep 17 00:00:00 2001
From: Micah Cowan <micah@cowan.name>
Date: Sat, 12 Apr 2008 13:09:43 -0700
Subject: [PATCH 4/4] Minor formatting adjustments.

---
 src/ChangeLog |  7 +++++++
 src/utils.c   | 26 ++++++++++++++------------
 2 files changed, 21 insertions(+), 12 deletions(-)

diff --git a/src/ChangeLog b/src/ChangeLog
index a2881858..4da5e1d3 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,10 @@
+2008-04-12  Micah Cowan  <micah@cowan.name>
+
+	* utils.c (aprintf): Minor formatting changes to Alex's code (80-
+	column limit, concatenated string literals, avoiding nesting
+	levels), and removed invocation of free (since we're aborting
+	anyway).
+
 2008-04-11  Alexander Dergachev  <cy6erbr4in@gmail.com>
 
 	* utils.c (aprintf): Now we are setting limits (1 Mb) for text
diff --git a/src/utils.c b/src/utils.c
index af621402..f4e56d82 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -178,7 +178,8 @@ aprintf (const char *fmt, ...)
   ret = vasprintf (&str, fmt, args);
   va_end (args);
   if (ret < 0 && errno == ENOMEM)
-    memfatal ("aprintf", UNKNOWN_ATTEMPTED_SIZE);  /* for consistency with xmalloc/xrealloc */
+    memfatal ("aprintf", UNKNOWN_ATTEMPTED_SIZE);  /* for consistency
+                                                      with xmalloc/xrealloc */
   else if (ret < 0)
     return NULL;
   return str;
@@ -208,19 +209,20 @@ aprintf (const char *fmt, ...)
       /* Else try again with a larger buffer. */
       if (n > -1)               /* C99 */
         size = n + 1;           /* precisely what is needed */
+      else if (size >= FMT_MAX_LENGTH)  /* We have a huge buffer, */
+        {                               /* maybe we have some wrong
+                                           format string? */
+          logprintf (LOG_ALWAYS, 
+                     _("%s: aprintf: text buffer is too big (%ld bytes), "
+                       "aborting.\n"),
+                     exec_name, size);  /* printout a log message */
+          abort ();                     /* and abort... */
+        }
       else
         {
-          if (size >= FMT_MAX_LENGTH)  /* We have a huge buffer, */
-            {                          /* maybe we have some wrong format string? */
-              free (str);              /* In this case we must free already allocated memory, */
-              logprintf (LOG_ALWAYS, 
-                         _("%s: aprintf: text buffer is too big (%ld bytes), \
-free already allocated memory and abort.\n"),
-                         exec_name, size);  /* printout a log message */
-              abort ();             /* and abort... */
-            }
-                                       /* else, we continue to grow our buffer. */
-          size <<= 1;                  /* twice the old size */
+          /* else, we continue to grow our
+           * buffer: Twice the old size. */
+          size <<= 1;
         }
       str = xrealloc (str, size);
     }