Home | History | Annotate | Line # | Download | only in libwrap
expandm.c revision 1.10
      1 /*	$NetBSD: expandm.c,v 1.10 2019/01/23 02:32:06 kre Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2019 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Christos Zoulas.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 #include <sys/cdefs.h>
     32 __RCSID("$NetBSD: expandm.c,v 1.10 2019/01/23 02:32:06 kre Exp $");
     33 
     34 #include <limits.h>
     35 #include <stdio.h>
     36 #include <string.h>
     37 #include <stdlib.h>
     38 #include <errno.h>
     39 
     40 #include "expandm.h"
     41 
     42 #ifdef TEST
     43 #undef INT_MAX
     44 #define INT_MAX 31
     45 #endif
     46 
     47 
     48 const char * __attribute__((__format_arg__(1)))
     49 expandm(const char *fmt, const char *sf, char **rbuf)
     50 {
     51 	const int err = errno;
     52 	const char *e = NULL;
     53 	char *buf, *m, *nbuf;
     54 	const char *ptr;
     55 
     56 	buf = NULL;
     57 	for (ptr = fmt; (m = strstr(ptr, "%m")) != NULL; ptr = m + 2) {
     58 		size_t cnt = 0;
     59 		size_t nlen;
     60 
     61 		for (char *p = m; p >= ptr && *p == '%'; p--)
     62 			cnt++;
     63 
     64 		nlen = (size_t)(m - ptr);
     65 		/*
     66 		 * we can't exceed INT_MAX because int is used as
     67 		 * a format width
     68 		 */
     69 		if (__predict_false(nlen >= INT_MAX)) {
     70 			size_t blen = buf ? strlen(buf) : 0;
     71 			size_t tlen = nlen + blen;
     72 
     73 			/*
     74 			 * We can't exceed PTRDIFF_MAX because we would
     75 			 * not be able to address the pointers
     76 			 */
     77 			if (tlen >= PTRDIFF_MAX)
     78 				goto out;
     79 
     80 			nbuf = realloc(buf, tlen + 1);
     81 			if (nbuf == NULL)
     82 				goto out;
     83 
     84 			memcpy(nbuf + blen, ptr, nlen);
     85 			nbuf[tlen] = '\0';
     86 			ptr += nlen;
     87 			buf = nbuf;
     88 		}
     89 
     90 		if (__predict_true(e == NULL && (cnt & 1) != 0))
     91 			e = strerror(err);
     92 		if (asprintf(&nbuf, "%s%.*s%s", buf ? buf : "",
     93 		    (int)(m - ptr), ptr, (cnt & 1) ? e : "%m") == -1)
     94 			goto out;
     95 		free(buf);
     96 		buf = nbuf;
     97 	}
     98 
     99 	if (asprintf(&nbuf, "%s%s%s", buf ? buf : "", ptr, sf ? sf : "") == -1)
    100 		goto out;
    101 
    102 	free(buf);
    103 	if (rbuf)
    104 		*rbuf = nbuf;
    105 	errno = err;
    106 	return nbuf;
    107 out:
    108 	free(buf);
    109 	if (rbuf)
    110 		*rbuf = NULL;
    111 	errno = err;
    112 	return fmt;
    113 }
    114 
    115 #ifdef TEST
    116 int
    117 main(int argc, char *argv[])
    118 {
    119 	errno = ERANGE;
    120 	printf("%s\n", expandm(argc > 1 ? argv[1] : "Message %%m=%m: %%%m%%",
    121 	    "...", NULL));
    122 	return 0;
    123 }
    124 #endif
    125