Home | History | Annotate | Line # | Download | only in libwrap
      1 /*	$NetBSD: expandm.c,v 1.13 2024/10/04 08:37:20 rillig 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.13 2024/10/04 08:37:20 rillig 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, *nbuf;
     54 	const char *ptr, *m;
     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 (const 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;
     72 
     73 			/*
     74 			 * if we would overflow a ptrdiff_t when computing
     75 			 * tlen, then don't bother.  The format string is
     76 			 * simply too large to be converted.
     77 			 */
     78 			if (blen >= PTRDIFF_MAX ||
     79 			    nlen >= PTRDIFF_MAX - blen ||
     80 			    nlen >= SIZE_T_MAX - blen)
     81 				goto out;
     82 
     83 			tlen = nlen + blen;
     84 
     85 			nbuf = realloc(buf, tlen + 1);
     86 			if (nbuf == NULL)
     87 				goto out;
     88 
     89 			memcpy(nbuf + blen, ptr, nlen);
     90 			nbuf[tlen] = '\0';
     91 			ptr += nlen;
     92 			buf = nbuf;
     93 		}
     94 
     95 		if (__predict_true(e == NULL && (cnt & 1) != 0))
     96 			e = strerror(err);
     97 		if (asprintf(&nbuf, "%s%.*s%s", buf ? buf : "",
     98 		    (int)(m - ptr), ptr, (cnt & 1) ? e : "%m") == -1)
     99 			goto out;
    100 		free(buf);
    101 		buf = nbuf;
    102 	}
    103 
    104 	if (asprintf(&nbuf, "%s%s%s", buf ? buf : "", ptr, sf ? sf : "") == -1)
    105 		goto out;
    106 
    107 	free(buf);
    108 	if (rbuf)
    109 		*rbuf = nbuf;
    110 	errno = err;
    111 	return nbuf;
    112 out:
    113 	free(buf);
    114 	if (rbuf)
    115 		*rbuf = NULL;
    116 	errno = err;
    117 	return fmt;
    118 }
    119 
    120 #ifdef TEST
    121 int
    122 main(int argc, char *argv[])
    123 {
    124 	errno = ERANGE;
    125 	printf("%s\n", expandm(argc > 1 ? argv[1] : "Message %%m=%m: %%%m%%",
    126 	    "...", NULL));
    127 	return 0;
    128 }
    129 #endif
    130