xref: /illumos-gate/usr/src/lib/libc/port/print/vsnprintf.c (revision 489f6310fe8952e87fc1dce8af87990fcfd90f18)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2025 Hans Rosenfeld
24  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
29 /*	  All Rights Reserved	*/
30 
31 #include "lint.h"
32 #include <mtlib.h>
33 #include <stdarg.h>
34 #include <values.h>
35 #include <errno.h>
36 #include <synch.h>
37 #include <thread.h>
38 #include <sys/types.h>
39 #include "print.h"
40 #include "libc.h"
41 #include <stdio_ext.h>
42 #include <upanic.h>
43 
44 #ifdef _C89_INTMAX32
45 #pragma redefine_extname vsnprintf _vsnprintf_c89
46 #pragma redefine_extname snprintf _snprintf_c89
47 #endif
48 
49 /*
50  * 32-bit shadow functions _vsnprintf_c89() and _snprintf_c89() are included
51  * here.
52  * When using the c89 compiler to build 32-bit applications, the size
53  * of intmax_t is 32-bits, otherwise the size of intmax_t is 64-bits.
54  * The shadow function uses 32-bit size of intmax_t for j conversion.
55  * The #pragma redefine_extname in <stdio.h> selects the proper routine
56  * at compile time for the user application.
57  * NOTE: the shadow functions only exist in the 32-bit library.
58  */
59 
60 int
61 vsnprintf(char *string, size_t n, const char *format, va_list ap)
62 {
63 	ssize_t count;
64 	FILE siop;
65 	size_t	max = MAXINT;
66 	unsigned char tmpbuf[1]; /* dummy buffer for _doprnt() if n == 0 */
67 
68 	/*
69 	 * The dummy FILE * created for vsnprintf has the _IOREAD
70 	 * flag set to distinguish it from printf and fprintf
71 	 * invocations. It also has the _IOWRT flag set to indicate
72 	 * it is writable, which is checked later by vfprintf().
73 	 */
74 	siop._flag = _IOWRT | _IOREAD;
75 	siop._cnt = n - 1;
76 	siop._base = siop._ptr = (unsigned char *)string;
77 #ifdef _LP64
78 	/*
79 	 * _bufend() (_realbufend()) should return NULL for v/snprintf,
80 	 * so PUT() macro in _doprnt() will bounds check.  For 32-bit,
81 	 * there is no _end field, and _realbufend() will return NULL
82 	 * since it cannot find the dummy FILE structure in the linked
83 	 * list of FILE strucutres.  See bug 4274368.
84 	 */
85 	siop._end = NULL;
86 #endif  /* _LP64 */
87 
88 	/*
89 	 * Mark the dummy FILE so that no locking is ever done.
90 	 */
91 	if (__fsetlocking(&siop, FSETLOCKING_BYCALLER) == -1)
92 		upanic(NULL, 0);	/* this should never happen */
93 
94 	if (n == 0) {
95 		/*
96 		 * When n==0, string may be NULL, so always use tmpbuf to
97 		 * guard for this case.  No bytes should be transmitted to
98 		 * the buffer when n==0, so using tmpbuf instead of string
99 		 * (ie, if string is not NULL) should not matter.
100 		 */
101 		siop._base = siop._ptr = tmpbuf;
102 		siop._cnt = 0;
103 	} else if (n > max) {
104 		errno = EOVERFLOW;
105 		return (EOF);
106 	}
107 
108 	count = vfprintf(&siop, format, ap);
109 
110 	*siop._ptr = '\0';	/* plant terminating null character */
111 
112 	return ((int)count);
113 }
114 
115 int
116 snprintf(char *string, size_t n, const char *format, ...)
117 {
118 	int count;
119 	va_list ap;
120 
121 	va_start(ap, format);
122 	count = vsnprintf(string, n, format, ap);
123 	va_end(ap);
124 
125 	return (count);
126 }
127