]> andersk Git - openssh.git/blame - bsd-snprintf.c
- (djm) Fix broken inet_ntoa check and ut_user/ut_name confusion, report
[openssh.git] / bsd-snprintf.c
CommitLineData
dad3b556 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 "config.h"
28
2647ae26 29#if defined(BROKEN_SNPRINTF) || !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF)
dad3b556 30
31#include <sys/param.h>
32#include <sys/types.h>
33#include <sys/mman.h>
34#include <signal.h>
35#include <stdio.h>
36#include <unistd.h>
37#include <string.h>
38#if __STDC__
39#include <stdarg.h>
40#include <stdlib.h>
41#else
42#include <varargs.h>
43#endif
44#include <setjmp.h>
45
46#ifndef roundup
e844f761 47#define roundup(x, y) ((((x)+((y)-1))/(y))*(y))
dad3b556 48#endif
49
50static int pgsize;
51static char *curobj;
52static int caught;
53static sigjmp_buf bail;
54
55#define EXTRABYTES 2 /* XXX: why 2? you don't want to know */
56
b6019d68 57#ifndef HAVE_GETPAGESIZE
58int
59getpagesize()
60{
61#ifdef EXEC_PAGESIZE
62 return EXEC_PAGESIZE;
63#else /* !EXEC_PAGESIZE */
64# ifdef NBPG
65# ifndef CLSIZE
66# define CLSIZE 1
67# endif /* No CLSIZE */
68 return NBPG * CLSIZE;
69# else /* !NBPG */
70 return NBPC;
71# endif /* NBPG */
72#endif /* EXEC_PAGESIZE */
73}
74#endif /* HAVE_GETPAGESIZE */
75
dad3b556 76static char *
77msetup(str, n)
78 char *str;
79 size_t n;
80{
81 char *e;
82
83 if (n == 0)
84 return NULL;
85 if (pgsize == 0)
86 pgsize = getpagesize();
87 curobj = (char *)malloc(n + EXTRABYTES + pgsize * 2);
88 if (curobj == NULL)
89 return NULL;
90 e = curobj + n + EXTRABYTES;
91 e = (char *)roundup((unsigned long)e, pgsize);
92 if (mprotect(e, pgsize, PROT_NONE) == -1) {
93 free(curobj);
94 curobj = NULL;
95 return NULL;
96 }
97 e = e - n - EXTRABYTES;
98 *e = '\0';
99 return (e);
100}
101
102static void
103mcatch()
104{
105 siglongjmp(bail, 1);
106}
107
108static void
109mcleanup(str, n, p)
110 char *str;
111 size_t n;
112 char *p;
113{
114 strncpy(str, p, n-1);
115 str[n-1] = '\0';
116 if (mprotect((caddr_t)(p + n + EXTRABYTES), pgsize,
117 PROT_READ|PROT_WRITE|PROT_EXEC) == -1)
118 mprotect((caddr_t)(p + n + EXTRABYTES), pgsize,
119 PROT_READ|PROT_WRITE);
120 free(curobj);
121}
122
2647ae26 123#if !defined(HAVE_VSNPRINTF) || defined(BROKEN_SNPRINTF)
dad3b556 124int
125vsnprintf(str, n, fmt, ap)
126 char *str;
127 size_t n;
128 char *fmt;
4d33e531 129 va_list ap;
dad3b556 130{
131 struct sigaction osa, nsa;
132 char *p;
133 int ret = n + 1; /* if we bail, indicated we overflowed */
134
135 memset(&nsa, 0, sizeof nsa);
136 nsa.sa_handler = mcatch;
137 sigemptyset(&nsa.sa_mask);
138
139 p = msetup(str, n);
140 if (p == NULL) {
141 *str = '\0';
142 return 0;
143 }
144 if (sigsetjmp(bail, 1) == 0) {
145 if (sigaction(SIGSEGV, &nsa, &osa) == -1) {
146 mcleanup(str, n, p);
147 return (0);
148 }
149 ret = vsprintf(p, fmt, ap);
150 }
151 mcleanup(str, n, p);
152 (void) sigaction(SIGSEGV, &osa, NULL);
153 return (ret);
154}
2647ae26 155#endif /* !defined(HAVE_VSNPRINTF) || defined(BROKEN_SNPRINTF) */
dad3b556 156
2647ae26 157#if !defined(HAVE_SNPRINTF) || defined(BROKEN_SNPRINTF)
847e8865 158int
159#if __STDC__
160snprintf(char *str, size_t n, char const *fmt, ...)
161#else
162snprintf(str, n, fmt, va_alist)
163 char *str;
164 size_t n;
165 char *fmt;
166 va_dcl
167#endif
168{
169 va_list ap;
170#if __STDC__
171 va_start(ap, fmt);
172#else
173 va_start(ap);
174#endif
175
176 return (vsnprintf(str, n, fmt, ap));
177 va_end(ap);
178}
2647ae26 179#endif /* !defined(HAVE_SNPRINTF) || defined(BROKEN_SNPRINTF) */
847e8865 180
2647ae26 181#endif /* defined(BROKEN_SNPRINTF) || !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF) */
This page took 0.08956 seconds and 5 git commands to generate.