1 |
adcroft |
1.1 |
/* |
2 |
|
|
* Revision 12: http://theos.com/~deraadt/snprintf.c |
3 |
|
|
* |
4 |
|
|
* Copyright (c) 1997 Theo de Raadt |
5 |
|
|
* |
6 |
|
|
* Redistribution and use in source and binary forms, with or without |
7 |
|
|
* modification, are permitted provided that the following conditions |
8 |
|
|
* are met: |
9 |
|
|
* 1. Redistributions of source code must retain the above copyright |
10 |
|
|
* notice, this list of conditions and the following disclaimer. |
11 |
|
|
* 2. Redistributions in binary form must reproduce the above copyright |
12 |
|
|
* notice, this list of conditions and the following disclaimer in the |
13 |
|
|
* documentation and/or other materials provided with the distribution. |
14 |
|
|
* |
15 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
16 |
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
17 |
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
18 |
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
19 |
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
20 |
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
21 |
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
22 |
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
23 |
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
24 |
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
25 |
|
|
*/ |
26 |
|
|
|
27 |
|
|
#include <sys/param.h> |
28 |
|
|
#include <sys/types.h> |
29 |
|
|
#include <sys/mman.h> |
30 |
|
|
#include <signal.h> |
31 |
|
|
#include <stdio.h> |
32 |
|
|
#if __STDC__ |
33 |
|
|
#include <stdarg.h> |
34 |
|
|
#include <stdlib.h> |
35 |
|
|
#else |
36 |
|
|
#include <varargs.h> |
37 |
|
|
#endif |
38 |
|
|
#include <setjmp.h> |
39 |
|
|
|
40 |
|
|
#ifndef roundup |
41 |
|
|
#define roundup(x, y) ((((x)+((y)-1))/(y))*(y)) |
42 |
|
|
#endif |
43 |
|
|
|
44 |
|
|
#ifdef __sgi |
45 |
|
|
#define size_t ssize_t |
46 |
|
|
#endif |
47 |
|
|
|
48 |
|
|
static int pgsize; |
49 |
|
|
static char *curobj; |
50 |
|
|
static int caught; |
51 |
|
|
static sigjmp_buf bail; |
52 |
|
|
|
53 |
|
|
#define EXTRABYTES 2 /* XXX: why 2? you don't want to know */ |
54 |
|
|
|
55 |
|
|
static char * |
56 |
|
|
msetup(str, n) |
57 |
|
|
char *str; |
58 |
|
|
size_t n; |
59 |
|
|
{ |
60 |
|
|
char *e; |
61 |
|
|
|
62 |
|
|
if (n == 0) |
63 |
|
|
return NULL; |
64 |
|
|
if (pgsize == 0) |
65 |
|
|
pgsize = getpagesize(); |
66 |
|
|
curobj = (char *)malloc(n + EXTRABYTES + pgsize * 2); |
67 |
|
|
if (curobj == NULL) |
68 |
|
|
return NULL; |
69 |
|
|
e = curobj + n + EXTRABYTES; |
70 |
|
|
e = (char *)roundup((unsigned long)e, pgsize); |
71 |
|
|
if (mprotect(e, pgsize, PROT_NONE) == -1) { |
72 |
|
|
free(curobj); |
73 |
|
|
curobj = NULL; |
74 |
|
|
return NULL; |
75 |
|
|
} |
76 |
|
|
e = e - n - EXTRABYTES; |
77 |
|
|
*e = '\0'; |
78 |
|
|
return (e); |
79 |
|
|
} |
80 |
|
|
|
81 |
|
|
static void |
82 |
|
|
mcatch() |
83 |
|
|
{ |
84 |
|
|
siglongjmp(bail, 1); |
85 |
|
|
} |
86 |
|
|
|
87 |
|
|
static void |
88 |
|
|
mcleanup(str, n, p) |
89 |
|
|
char *str; |
90 |
|
|
size_t n; |
91 |
|
|
char *p; |
92 |
|
|
{ |
93 |
|
|
strncpy(str, p, n-1); |
94 |
|
|
str[n-1] = '\0'; |
95 |
|
|
if (mprotect((caddr_t)(p + n + EXTRABYTES), pgsize, |
96 |
|
|
PROT_READ|PROT_WRITE|PROT_EXEC) == -1) |
97 |
|
|
mprotect((caddr_t)(p + n + EXTRABYTES), pgsize, |
98 |
|
|
PROT_READ|PROT_WRITE); |
99 |
|
|
free(curobj); |
100 |
|
|
} |
101 |
|
|
|
102 |
|
|
int |
103 |
|
|
#if __STDC__ |
104 |
|
|
vsnprintf(char *str, size_t n, char const *fmt, va_list ap) |
105 |
|
|
#else |
106 |
|
|
vsnprintf(str, n, fmt, ap) |
107 |
|
|
char *str; |
108 |
|
|
size_t n; |
109 |
|
|
char *fmt; |
110 |
|
|
char *ap; |
111 |
|
|
#endif |
112 |
|
|
{ |
113 |
|
|
struct sigaction osa, nsa; |
114 |
|
|
char *p; |
115 |
|
|
int ret = n + 1; /* if we bail, indicated we overflowed */ |
116 |
|
|
|
117 |
|
|
memset(&nsa, 0, sizeof nsa); |
118 |
|
|
nsa.sa_handler = mcatch; |
119 |
|
|
sigemptyset(&nsa.sa_mask); |
120 |
|
|
|
121 |
|
|
p = msetup(str, n); |
122 |
|
|
if (p == NULL) { |
123 |
|
|
*str = '\0'; |
124 |
|
|
return 0; |
125 |
|
|
} |
126 |
|
|
if (sigsetjmp(bail, 1) == 0) { |
127 |
|
|
if (sigaction(SIGSEGV, &nsa, &osa) == -1) { |
128 |
|
|
mcleanup(str, n, p); |
129 |
|
|
return (0); |
130 |
|
|
} |
131 |
|
|
ret = vsprintf(p, fmt, ap); |
132 |
|
|
} |
133 |
|
|
mcleanup(str, n, p); |
134 |
|
|
(void) sigaction(SIGSEGV, &osa, NULL); |
135 |
|
|
return (ret); |
136 |
|
|
} |
137 |
|
|
|
138 |
|
|
int |
139 |
|
|
#if __STDC__ |
140 |
|
|
snprintf(char *str, size_t n, char const *fmt, ...) |
141 |
|
|
#else |
142 |
|
|
snprintf(str, n, fmt, va_alist) |
143 |
|
|
char *str; |
144 |
|
|
size_t n; |
145 |
|
|
char *fmt; |
146 |
|
|
va_dcl |
147 |
|
|
#endif |
148 |
|
|
{ |
149 |
|
|
va_list ap; |
150 |
|
|
#if __STDC__ |
151 |
|
|
va_start(ap, fmt); |
152 |
|
|
#else |
153 |
|
|
va_start(ap); |
154 |
|
|
#endif |
155 |
|
|
|
156 |
|
|
return (vsnprintf(str, n, fmt, ap)); |
157 |
|
|
va_end(ap); |
158 |
|
|
} |
159 |
|
|
|
160 |
|
|
|
161 |
|
|
|