Sat Nov 16 12:21:06 2019 UTC ()
autofs: Fix segfault that could occur on "automount -LL"


(tkusumi)
diff -r1.2 -r1.3 src/usr.sbin/autofs/common.c

cvs diff -r1.2 -r1.3 src/usr.sbin/autofs/common.c (expand / switch to unified diff)

--- src/usr.sbin/autofs/common.c 2019/11/16 11:56:29 1.2
+++ src/usr.sbin/autofs/common.c 2019/11/16 12:21:06 1.3
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: common.c,v 1.2 2019/11/16 11:56:29 tkusumi Exp $ */ 1/* $NetBSD: common.c,v 1.3 2019/11/16 12:21:06 tkusumi Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 2017 The NetBSD Foundation, Inc. 4 * Copyright (c) 2017 The NetBSD Foundation, Inc.
5 * Copyright (c) 2016 The DragonFly Project 5 * Copyright (c) 2016 The DragonFly Project
6 * Copyright (c) 2014 The FreeBSD Foundation 6 * Copyright (c) 2014 The FreeBSD Foundation
7 * All rights reserved. 7 * All rights reserved.
8 * 8 *
9 * This code is derived from software contributed to The NetBSD Foundation 9 * This code is derived from software contributed to The NetBSD Foundation
10 * by Tomohiro Kusumi <kusumi.tomohiro@gmail.com>. 10 * by Tomohiro Kusumi <kusumi.tomohiro@gmail.com>.
11 * 11 *
12 * This software was developed by Edward Tomasz Napierala under sponsorship 12 * This software was developed by Edward Tomasz Napierala under sponsorship
13 * from the FreeBSD Foundation. 13 * from the FreeBSD Foundation.
14 * 14 *
@@ -26,27 +26,27 @@ @@ -26,27 +26,27 @@
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE. 34 * SUCH DAMAGE.
35 * 35 *
36 * $FreeBSD: head/usr.sbin/autofs/common.c 303527 2016-07-30 01:10:05Z bapt $ 36 * $FreeBSD: head/usr.sbin/autofs/common.c 303527 2016-07-30 01:10:05Z bapt $
37 */ 37 */
38#include <sys/cdefs.h> 38#include <sys/cdefs.h>
39__RCSID("$NetBSD: common.c,v 1.2 2019/11/16 11:56:29 tkusumi Exp $"); 39__RCSID("$NetBSD: common.c,v 1.3 2019/11/16 12:21:06 tkusumi Exp $");
40 40
41#include <sys/types.h> 41#include <sys/types.h>
42#include <sys/stat.h> 42#include <sys/stat.h>
43#include <assert.h> 43#include <assert.h>
44#include <err.h> 44#include <err.h>
45#include <errno.h> 45#include <errno.h>
46#include <fcntl.h> 46#include <fcntl.h>
47#include <libgen.h> 47#include <libgen.h>
48#include <paths.h> 48#include <paths.h>
49#include <stdio.h> 49#include <stdio.h>
50#include <stdlib.h> 50#include <stdlib.h>
51#include <string.h> 51#include <string.h>
52#include <unistd.h> 52#include <unistd.h>
@@ -347,41 +347,43 @@ expand_ampersand(char *string, const cha @@ -347,41 +347,43 @@ expand_ampersand(char *string, const cha
347 if (backslashed) { 347 if (backslashed) {
348 backslashed = false; 348 backslashed = false;
349 continue; 349 continue;
350 } 350 }
351 backslashed = false; 351 backslashed = false;
352 if (c != '&') 352 if (c != '&')
353 continue; 353 continue;
354 354
355 /* 355 /*
356 * The 'before_len' variable contains the number 356 * The 'before_len' variable contains the number
357 * of characters before the '&'. 357 * of characters before the '&'.
358 */ 358 */
359 before_len = i; 359 before_len = i;
360 //assert(i + 1 < strlen(string)); 360 //assert(i < strlen(string));
361 361
362 ret = asprintf(&expanded, "%.*s%s%s", 362 ret = asprintf(&expanded, "%.*s%s%s",
363 (int)before_len, string, key, string + before_len + 1); 363 (int)before_len, string, key, string + before_len + 1);
364 if (ret < 0) 364 if (ret < 0)
365 log_err(1, "asprintf"); 365 log_err(1, "asprintf");
366 366
367 //log_debugx("\"%s\" expanded with key \"%s\" to \"%s\"", 367 //log_debugx("\"%s\" expanded with key \"%s\" to \"%s\"",
368 // string, key, expanded); 368 // string, key, expanded);
369 369
370 /* 370 /*
371 * Figure out where to start searching for next variable. 371 * Figure out where to start searching for next variable.
372 */ 372 */
373 string = expanded; 373 string = expanded;
374 i = before_len + strlen(key); 374 i = before_len + strlen(key);
 375 if (i == strlen(string))
 376 break;
375 backslashed = false; 377 backslashed = false;
376 //assert(i < strlen(string)); 378 //assert(i < strlen(string));
377 } 379 }
378 380
379 return expanded; 381 return expanded;
380} 382}
381 383
382/* 384/*
383 * Expand "&" in n_location. If the key is NULL, try to use 385 * Expand "&" in n_location. If the key is NULL, try to use
384 * key from map entries themselves. Keep in mind that maps 386 * key from map entries themselves. Keep in mind that maps
385 * consist of tho levels of node structures, the key is one 387 * consist of tho levels of node structures, the key is one
386 * level up. 388 * level up.
387 * 389 *