Thu Jul 28 00:48:21 2011 UTC ()
- fix memory allocation botch in wide strings
- check mbstowcs return code


(christos)
diff -r1.5 -r1.6 src/lib/libedit/chartype.c

cvs diff -r1.5 -r1.6 src/lib/libedit/chartype.c (expand / switch to unified diff)

--- src/lib/libedit/chartype.c 2011/07/27 02:18:30 1.5
+++ src/lib/libedit/chartype.c 2011/07/28 00:48:21 1.6
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: chartype.c,v 1.5 2011/07/27 02:18:30 christos Exp $ */ 1/* $NetBSD: chartype.c,v 1.6 2011/07/28 00:48:21 christos Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 2009 The NetBSD Foundation, Inc. 4 * Copyright (c) 2009 The NetBSD Foundation, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
@@ -28,52 +28,52 @@ @@ -28,52 +28,52 @@
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE. 33 * POSSIBILITY OF SUCH DAMAGE.
34 */ 34 */
35 35
36/* 36/*
37 * chartype.c: character classification and meta information 37 * chartype.c: character classification and meta information
38 */ 38 */
39#include "config.h" 39#include "config.h"
40#if !defined(lint) && !defined(SCCSID) 40#if !defined(lint) && !defined(SCCSID)
41__RCSID("$NetBSD: chartype.c,v 1.5 2011/07/27 02:18:30 christos Exp $"); 41__RCSID("$NetBSD: chartype.c,v 1.6 2011/07/28 00:48:21 christos Exp $");
42#endif /* not lint && not SCCSID */ 42#endif /* not lint && not SCCSID */
43#include "el.h" 43#include "el.h"
44#include <stdlib.h> 44#include <stdlib.h>
45 45
46#define CT_BUFSIZ 1024 46#define CT_BUFSIZ 1024
47 47
48#ifdef WIDECHAR 48#ifdef WIDECHAR
49protected void 49protected void
50ct_conv_buff_resize(ct_buffer_t *conv, size_t mincsize, size_t minwsize) 50ct_conv_buff_resize(ct_buffer_t *conv, size_t mincsize, size_t minwsize)
51{ 51{
52 void *p; 52 void *p;
53 if (mincsize > conv->csize) { 53 if (mincsize > conv->csize) {
54 conv->csize = mincsize; 54 conv->csize = mincsize;
55 p = el_realloc(conv->cbuff, conv->csize); 55 p = el_realloc(conv->cbuff, conv->csize * sizeof(char));
56 if (p == NULL) { 56 if (p == NULL) {
57 conv->csize = 0; 57 conv->csize = 0;
58 el_free(conv->cbuff); 58 el_free(conv->cbuff);
59 conv->cbuff = NULL; 59 conv->cbuff = NULL;
60 } else  60 } else
61 conv->cbuff = p; 61 conv->cbuff = p;
62 } 62 }
63 63
64 if (minwsize > conv->wsize) { 64 if (minwsize > conv->wsize) {
65 conv->wsize = minwsize; 65 conv->wsize = minwsize;
66 p = el_realloc(conv->wbuff, conv->wsize); 66 p = el_realloc(conv->wbuff, conv->wsize * sizeof(Char));
67 if (p == NULL) { 67 if (p == NULL) {
68 conv->wsize = 0; 68 conv->wsize = 0;
69 el_free(conv->wbuff); 69 el_free(conv->wbuff);
70 conv->wbuff = NULL; 70 conv->wbuff = NULL;
71 } else 71 } else
72 conv->wbuff = p; 72 conv->wbuff = p;
73 } 73 }
74} 74}
75 75
76 76
77public char * 77public char *
78ct_encode_string(const Char *s, ct_buffer_t *conv) 78ct_encode_string(const Char *s, ct_buffer_t *conv)
79{ 79{
@@ -109,27 +109,29 @@ ct_encode_string(const Char *s, ct_buffe @@ -109,27 +109,29 @@ ct_encode_string(const Char *s, ct_buffe
109 109
110public Char * 110public Char *
111ct_decode_string(const char *s, ct_buffer_t *conv) 111ct_decode_string(const char *s, ct_buffer_t *conv)
112{ 112{
113 size_t len = 0; 113 size_t len = 0;
114 114
115 if (!s) 115 if (!s)
116 return NULL; 116 return NULL;
117 if (!conv->wbuff) 117 if (!conv->wbuff)
118 ct_conv_buff_resize(conv, 0, CT_BUFSIZ); 118 ct_conv_buff_resize(conv, 0, CT_BUFSIZ);
119 if (!conv->wbuff) 119 if (!conv->wbuff)
120 return NULL; 120 return NULL;
121 121
122 len = ct_mbstowcs(0, s, 0); 122 len = ct_mbstowcs(NULL, s, 0);
 123 if (len == (size_t)-1)
 124 return NULL;
123 if (len > conv->wsize) 125 if (len > conv->wsize)
124 ct_conv_buff_resize(conv, 0, len + 1); 126 ct_conv_buff_resize(conv, 0, len + 1);
125 if (!conv->wbuff) 127 if (!conv->wbuff)
126 return NULL; 128 return NULL;
127 ct_mbstowcs(conv->wbuff, s, conv->wsize); 129 ct_mbstowcs(conv->wbuff, s, conv->wsize);
128 return conv->wbuff; 130 return conv->wbuff;
129} 131}
130 132
131 133
132protected Char ** 134protected Char **
133ct_decode_argv(int argc, const char *argv[], ct_buffer_t *conv) 135ct_decode_argv(int argc, const char *argv[], ct_buffer_t *conv)
134{ 136{
135 size_t bufspace; 137 size_t bufspace;