Thu Sep 12 07:28:28 2013 UTC ()
Fix pkg_info -X segfault when last entry wasn't terminated with \n.
Addresses PR 48207.


(wiz)
diff -r1.9 -r1.10 pkgsrc/pkgtools/pkg_install/files/lib/var.c

cvs diff -r1.9 -r1.10 pkgsrc/pkgtools/pkg_install/files/lib/var.c (expand / switch to unified diff)

--- pkgsrc/pkgtools/pkg_install/files/lib/var.c 2013/05/17 07:27:29 1.9
+++ pkgsrc/pkgtools/pkg_install/files/lib/var.c 2013/09/12 07:28:28 1.10
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: var.c,v 1.9 2013/05/17 07:27:29 martin Exp $ */ 1/* $NetBSD: var.c,v 1.10 2013/09/12 07:28:28 wiz Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 2005, 2008 The NetBSD Foundation, Inc. 4 * Copyright (c) 2005, 2008 The NetBSD Foundation, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to The NetBSD Foundation 7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Dieter Baron, Thomas Klausner, Johnny Lam, and Joerg Sonnenberger. 8 * by Dieter Baron, Thomas Klausner, Johnny Lam, and Joerg Sonnenberger.
9 * 9 *
10 * Redistribution and use in source and binary forms, with or without 10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions 11 * modification, are permitted provided that the following conditions
12 * are met: 12 * are met:
13 * 1. Redistributions of source code must retain the above copyright 13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer. 14 * notice, this list of conditions and the following disclaimer.
@@ -29,63 +29,63 @@ @@ -29,63 +29,63 @@
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE. 32 * POSSIBILITY OF SUCH DAMAGE.
33 */ 33 */
34 34
35#if HAVE_CONFIG_H 35#if HAVE_CONFIG_H
36#include "config.h" 36#include "config.h"
37#endif 37#endif
38#include <nbcompat.h> 38#include <nbcompat.h>
39#if HAVE_SYS_CDEFS_H 39#if HAVE_SYS_CDEFS_H
40#include <sys/cdefs.h> 40#include <sys/cdefs.h>
41#endif 41#endif
42__RCSID("$NetBSD: var.c,v 1.9 2013/05/17 07:27:29 martin Exp $"); 42__RCSID("$NetBSD: var.c,v 1.10 2013/09/12 07:28:28 wiz Exp $");
43 43
44#if HAVE_SYS_STAT_H 44#if HAVE_SYS_STAT_H
45#include <sys/stat.h> 45#include <sys/stat.h>
46#endif 46#endif
47#if HAVE_ERR_H 47#if HAVE_ERR_H
48#include <err.h> 48#include <err.h>
49#endif 49#endif
50#if HAVE_ERRNO_H 50#if HAVE_ERRNO_H
51#include <errno.h> 51#include <errno.h>
52#endif 52#endif
53#if HAVE_STDIO_H 53#if HAVE_STDIO_H
54#include <stdio.h> 54#include <stdio.h>
55#endif 55#endif
56 56
57#include "lib.h" 57#include "lib.h"
58 58
59static const char *var_cmp(const char *, size_t, const char *, size_t); 59static const char *var_cmp(const char *, size_t, const char *, size_t);
60static void var_print(FILE *, const char *, const char *); 60static void var_print(FILE *, const char *, const char *);
61 61
62/* 62/*
63 * Copy the specified varibales from the file fname to stdout. 63 * Copy the specified variables from the file fname to stdout.
64 */ 64 */
65int 65int
66var_copy_list(const char *buf, const char **variables) 66var_copy_list(const char *buf, const char **variables)
67{ 67{
68 const char *eol, *next; 68 const char *eol, *next;
69 size_t len; 69 size_t len;
70 int i; 70 int i;
71 71
72 for (; *buf; buf = next) { 72 for (; *buf; buf = next) {
73 if ((eol = strchr(buf, '\n')) != NULL) { 73 if ((eol = strchr(buf, '\n')) != NULL) {
74 next = eol + 1; 74 next = eol + 1;
75 len = eol - buf; 75 len = eol - buf;
76 } else { 76 } else {
77 next = eol; 
78 len = strlen(buf); 77 len = strlen(buf);
 78 next = buf + len;
79 } 79 }
80 80
81 for (i=0; variables[i]; i++) { 81 for (i=0; variables[i]; i++) {
82 if (var_cmp(buf, len, variables[i], 82 if (var_cmp(buf, len, variables[i],
83 strlen(variables[i])) != NULL) { 83 strlen(variables[i])) != NULL) {
84 printf("%.*s\n", (int)len, buf); 84 printf("%.*s\n", (int)len, buf);
85 break; 85 break;
86 } 86 }
87 } 87 }
88 } 88 }
89 return 0; 89 return 0;
90} 90}
91 91