* build-aux/depcomp, build-aux/install-sh, build-aux/mdate-sh,
[gnulib.git] / lib / argp-fmtstream.c
index c6d79a5..a97f20c 100644 (file)
@@ -15,7 +15,7 @@
 
    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation,
-   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
+   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
 
 /* This package emulates glibc `line_wrap_stream' semantics for systems that
    don't have that.  */
@@ -384,10 +384,11 @@ __argp_fmtstream_ensure (struct argp_fmtstream *fs, size_t amount)
       if ((size_t) (fs->end - fs->buf) < amount)
        /* Gotta grow the buffer.  */
        {
-         size_t new_size = fs->end - fs->buf + amount;
-         char *new_buf = realloc (fs->buf, new_size);
+         size_t old_size = fs->end - fs->buf;
+         size_t new_size = old_size + amount;
+         char *new_buf;
 
-         if (! new_buf)
+         if (new_size < old_size || ! (new_buf = realloc (fs->buf, new_size)))
            {
              __set_errno (ENOMEM);
              return 0;