Mon Nov 27 23:54:28 2017 UTC ()
use calloc rather than malloc + memset 0


(maya)
diff -r1.12 -r1.13 src/lib/libc/stdlib/strfmon.c

cvs diff -r1.12 -r1.13 src/lib/libc/stdlib/strfmon.c (expand / switch to unified diff)

--- src/lib/libc/stdlib/strfmon.c 2017/11/27 22:43:07 1.12
+++ src/lib/libc/stdlib/strfmon.c 2017/11/27 23:54:28 1.13
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: strfmon.c,v 1.12 2017/11/27 22:43:07 christos Exp $ */ 1/* $NetBSD: strfmon.c,v 1.13 2017/11/27 23:54:28 maya Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 2001 Alexey Zelkin <phantom@FreeBSD.org> 4 * Copyright (c) 2001 Alexey Zelkin <phantom@FreeBSD.org>
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.
@@ -22,27 +22,27 @@ @@ -22,27 +22,27 @@
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE. 26 * SUCH DAMAGE.
27 * 27 *
28 */ 28 */
29 29
30#include <sys/cdefs.h> 30#include <sys/cdefs.h>
31#if defined(LIBC_SCCS) && !defined(lint) 31#if defined(LIBC_SCCS) && !defined(lint)
32#if 0 32#if 0
33__FBSDID("$FreeBSD: src/lib/libc/stdlib/strfmon.c,v 1.14 2003/03/20 08:18:55 ache Exp $"); 33__FBSDID("$FreeBSD: src/lib/libc/stdlib/strfmon.c,v 1.14 2003/03/20 08:18:55 ache Exp $");
34#else 34#else
35__RCSID("$NetBSD: strfmon.c,v 1.12 2017/11/27 22:43:07 christos Exp $"); 35__RCSID("$NetBSD: strfmon.c,v 1.13 2017/11/27 23:54:28 maya Exp $");
36#endif 36#endif
37#endif /* LIBC_SCCS and not lint */ 37#endif /* LIBC_SCCS and not lint */
38 38
39#include "namespace.h" 39#include "namespace.h"
40 40
41#include <sys/types.h> 41#include <sys/types.h>
42#include <assert.h> 42#include <assert.h>
43#include <ctype.h> 43#include <ctype.h>
44#include <errno.h> 44#include <errno.h>
45#include <limits.h> 45#include <limits.h>
46#include <locale.h> 46#include <locale.h>
47#include <monetary.h> 47#include <monetary.h>
48#include <stdarg.h> 48#include <stdarg.h>
@@ -546,32 +546,31 @@ __format_grouped_double(struct lconv *lc @@ -546,32 +546,31 @@ __format_grouped_double(struct lconv *lc
546 } 546 }
547 547
548 if (*flags & NEED_GROUPING) 548 if (*flags & NEED_GROUPING)
549 left_prec += get_groups(left_prec, grouping); 549 left_prec += get_groups(left_prec, grouping);
550 550
551 /* convert to string */ 551 /* convert to string */
552 avalue_size = asprintf(&avalue, "%*.*f", left_prec + right_prec + 1, 552 avalue_size = asprintf(&avalue, "%*.*f", left_prec + right_prec + 1,
553 right_prec, value); 553 right_prec, value);
554 if (avalue_size < 0) 554 if (avalue_size < 0)
555 return (NULL); 555 return (NULL);
556 556
557 /* make sure that we've enough space for result string */ 557 /* make sure that we've enough space for result string */
558 bufsize = avalue_size * 2 + 1; 558 bufsize = avalue_size * 2 + 1;
559 rslt = malloc(bufsize); 559 rslt = calloc(1, bufsize);
560 if (rslt == NULL) { 560 if (rslt == NULL) {
561 free(avalue); 561 free(avalue);
562 return (NULL); 562 return (NULL);
563 } 563 }
564 memset(rslt, 0, bufsize); 
565 bufend = rslt + bufsize - 1; /* reserve space for trailing '\0' */ 564 bufend = rslt + bufsize - 1; /* reserve space for trailing '\0' */
566 565
567 /* skip spaces at beggining */ 566 /* skip spaces at beggining */
568 padded = 0; 567 padded = 0;
569 while (avalue[padded] == ' ') { 568 while (avalue[padded] == ' ') {
570 padded++; 569 padded++;
571 avalue_size--; 570 avalue_size--;
572 } 571 }
573 572
574 if (right_prec > 0) { 573 if (right_prec > 0) {
575 bufend -= right_prec; 574 bufend -= right_prec;
576 memcpy(bufend, avalue + avalue_size+padded-right_prec, 575 memcpy(bufend, avalue + avalue_size+padded-right_prec,
577 (size_t) right_prec); 576 (size_t) right_prec);