From c76d71fc1aeaa21f58b917c26d1991ffb550fda5 Mon Sep 17 00:00:00 2001 From: christos Date: Wed, 27 Jul 2011 02:18:30 +0000 Subject: [PATCH] fix buffer growing code. --- lib/libedit/chartype.c | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/lib/libedit/chartype.c b/lib/libedit/chartype.c index 3a539d468eb0..82159ba57550 100644 --- a/lib/libedit/chartype.c +++ b/lib/libedit/chartype.c @@ -1,4 +1,4 @@ -/* $NetBSD: chartype.c,v 1.4 2010/04/15 00:55:57 christos Exp $ */ +/* $NetBSD: chartype.c,v 1.5 2011/07/27 02:18:30 christos Exp $ */ /*- * Copyright (c) 2009 The NetBSD Foundation, Inc. @@ -38,7 +38,7 @@ */ #include "config.h" #if !defined(lint) && !defined(SCCSID) -__RCSID("$NetBSD: chartype.c,v 1.4 2010/04/15 00:55:57 christos Exp $"); +__RCSID("$NetBSD: chartype.c,v 1.5 2011/07/27 02:18:30 christos Exp $"); #endif /* not lint && not SCCSID */ #include "el.h" #include @@ -89,27 +89,20 @@ ct_encode_string(const Char *s, ct_buffer_t *conv) dst = conv->cbuff; while (*s) { - used = ct_encode_char(dst, (int)(conv->csize - - (dst - conv->cbuff)), *s); - if (used == -1) { /* failed to encode, need more buffer space */ + used = conv->csize - (dst - conv->cbuff); + if (used < 5) { used = dst - conv->cbuff; ct_conv_buff_resize(conv, conv->csize + CT_BUFSIZ, 0); if (!conv->cbuff) return NULL; dst = conv->cbuff + used; - /* don't increment s here - we want to retry it! */ } - else - ++s; + used = ct_encode_char(dst, 5, *s); + if (used == -1) /* failed to encode, need more buffer space */ + abort(); + ++s; dst += used; } - if (dst >= (conv->cbuff + conv->csize)) { - used = dst - conv->cbuff; - ct_conv_buff_resize(conv, conv->csize + 1, 0); - if (!conv->cbuff) - return NULL; - dst = conv->cbuff + used; - } *dst = '\0'; return conv->cbuff; }