Thu Nov 12 22:11:03 2020 UTC ()
Don't use static buffers to store format strings; this is a multi-threaded
program and if we print from multiple threads we can end up overwriting the
formats while printf is formating and that can end up in tears.


(christos)
diff -r1.3 -r1.4 src/external/mpl/dhcp/dist/omapip/errwarn.c

cvs diff -r1.3 -r1.4 src/external/mpl/dhcp/dist/omapip/errwarn.c (expand / switch to unified diff)

--- src/external/mpl/dhcp/dist/omapip/errwarn.c 2020/08/03 21:10:57 1.3
+++ src/external/mpl/dhcp/dist/omapip/errwarn.c 2020/11/12 22:11:03 1.4
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: errwarn.c,v 1.3 2020/08/03 21:10:57 christos Exp $ */ 1/* $NetBSD: errwarn.c,v 1.4 2020/11/12 22:11:03 christos Exp $ */
2 2
3/* errwarn.c 3/* errwarn.c
4 4
5 Errors and warnings... */ 5 Errors and warnings... */
6 6
7/* 7/*
8 * Copyright (c) 1995 RadioMail Corporation. 8 * Copyright (c) 1995 RadioMail Corporation.
9 * Copyright (c) 2004-2019 by Internet Systems Consortium, Inc. ("ISC") 9 * Copyright (c) 2004-2019 by Internet Systems Consortium, Inc. ("ISC")
10 * Copyright (c) 1996-2003 by Internet Software Consortium 10 * Copyright (c) 1996-2003 by Internet Software Consortium
11 * 11 *
12 * This Source Code Form is subject to the terms of the Mozilla Public 12 * This Source Code Form is subject to the terms of the Mozilla Public
13 * License, v. 2.0. If a copy of the MPL was not distributed with this 13 * License, v. 2.0. If a copy of the MPL was not distributed with this
14 * file, You can obtain one at http://mozilla.org/MPL/2.0/. 14 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
@@ -24,50 +24,50 @@ @@ -24,50 +24,50 @@
24 * Internet Systems Consortium, Inc. 24 * Internet Systems Consortium, Inc.
25 * 950 Charter Street 25 * 950 Charter Street
26 * Redwood City, CA 94063 26 * Redwood City, CA 94063
27 * <info@isc.org> 27 * <info@isc.org>
28 * https://www.isc.org/ 28 * https://www.isc.org/
29 * 29 *
30 * This software was written for RadioMail Corporation by Ted Lemon 30 * This software was written for RadioMail Corporation by Ted Lemon
31 * under a contract with Vixie Enterprises. Further modifications have 31 * under a contract with Vixie Enterprises. Further modifications have
32 * been made for Internet Systems Consortium under a contract 32 * been made for Internet Systems Consortium under a contract
33 * with Vixie Laboratories. 33 * with Vixie Laboratories.
34 */ 34 */
35 35
36#include <sys/cdefs.h> 36#include <sys/cdefs.h>
37__RCSID("$NetBSD: errwarn.c,v 1.3 2020/08/03 21:10:57 christos Exp $"); 37__RCSID("$NetBSD: errwarn.c,v 1.4 2020/11/12 22:11:03 christos Exp $");
38 38
39#include "dhcpd.h" 39#include "dhcpd.h"
40 40
41#include <omapip/omapip_p.h> 41#include <omapip/omapip_p.h>
42#include <errno.h> 42#include <errno.h>
43#include <syslog.h> 43#include <syslog.h>
44 44
45#ifdef DEBUG 45#ifdef DEBUG
46int log_perror = -1; 46int log_perror = -1;
47#else 47#else
48int log_perror = 1; 48int log_perror = 1;
49#endif 49#endif
50void (*log_cleanup) (void); 50void (*log_cleanup) (void);
51 51
52#define CVT_BUF_MAX 1023 52#define CVT_BUF_MAX 1023
53static char mbuf [CVT_BUF_MAX + 1]; 
54static char fbuf [CVT_BUF_MAX + 1]; 
55 53
56/* Log an error message, then exit... */ 54/* Log an error message, then exit... */
57 55
58void log_fatal (const char * fmt, ... ) 56void log_fatal (const char * fmt, ... )
59{ 57{
60 va_list list; 58 va_list list;
 59 char mbuf [CVT_BUF_MAX + 1];
 60 char fbuf [CVT_BUF_MAX + 1];
61 61
62 do_percentm (fbuf, sizeof fbuf, fmt); 62 do_percentm (fbuf, sizeof fbuf, fmt);
63 63
64 /* %Audit% This is log output. %2004.06.17,Safe% 64 /* %Audit% This is log output. %2004.06.17,Safe%
65 * If we truncate we hope the user can get a hint from the log. 65 * If we truncate we hope the user can get a hint from the log.
66 */ 66 */
67 va_start (list, fmt); 67 va_start (list, fmt);
68 vsnprintf (mbuf, sizeof mbuf, fbuf, list); 68 vsnprintf (mbuf, sizeof mbuf, fbuf, list);
69 va_end (list); 69 va_end (list);
70 70
71#ifndef DEBUG 71#ifndef DEBUG
72 syslog (LOG_ERR, "%s", mbuf); 72 syslog (LOG_ERR, "%s", mbuf);
73#endif 73#endif
@@ -86,26 +86,28 @@ void log_fatal (const char * fmt, ... ) @@ -86,26 +86,28 @@ void log_fatal (const char * fmt, ... )
86 log_error ("process and the information we find helpful for debugging."); 86 log_error ("process and the information we find helpful for debugging.");
87 log_error ("%s", ""); 87 log_error ("%s", "");
88 log_error ("exiting."); 88 log_error ("exiting.");
89 89
90 if (log_cleanup) 90 if (log_cleanup)
91 (*log_cleanup) (); 91 (*log_cleanup) ();
92 exit (1); 92 exit (1);
93} 93}
94 94
95/* Log an error message... */ 95/* Log an error message... */
96 96
97int log_error (const char * fmt, ...) 97int log_error (const char * fmt, ...)
98{ 98{
 99 char mbuf [CVT_BUF_MAX + 1];
 100 char fbuf [CVT_BUF_MAX + 1];
99 va_list list; 101 va_list list;
100 102
101 do_percentm (fbuf, sizeof fbuf, fmt); 103 do_percentm (fbuf, sizeof fbuf, fmt);
102 104
103 /* %Audit% This is log output. %2004.06.17,Safe% 105 /* %Audit% This is log output. %2004.06.17,Safe%
104 * If we truncate we hope the user can get a hint from the log. 106 * If we truncate we hope the user can get a hint from the log.
105 */ 107 */
106 va_start (list, fmt); 108 va_start (list, fmt);
107 vsnprintf (mbuf, sizeof mbuf, fbuf, list); 109 vsnprintf (mbuf, sizeof mbuf, fbuf, list);
108 va_end (list); 110 va_end (list);
109 111
110#ifndef DEBUG 112#ifndef DEBUG
111 syslog (LOG_ERR, "%s", mbuf); 113 syslog (LOG_ERR, "%s", mbuf);
@@ -113,26 +115,28 @@ int log_error (const char * fmt, ...) @@ -113,26 +115,28 @@ int log_error (const char * fmt, ...)
113 115
114 if (log_perror) { 116 if (log_perror) {
115 IGNORE_RET (write (STDERR_FILENO, mbuf, strlen (mbuf))); 117 IGNORE_RET (write (STDERR_FILENO, mbuf, strlen (mbuf)));
116 IGNORE_RET (write (STDERR_FILENO, "\n", 1)); 118 IGNORE_RET (write (STDERR_FILENO, "\n", 1));
117 } 119 }
118 120
119 return 0; 121 return 0;
120} 122}
121 123
122/* Log a note... */ 124/* Log a note... */
123 125
124int log_info (const char *fmt, ...) 126int log_info (const char *fmt, ...)
125{ 127{
 128 char mbuf [CVT_BUF_MAX + 1];
 129 char fbuf [CVT_BUF_MAX + 1];
126 va_list list; 130 va_list list;
127 131
128 do_percentm (fbuf, sizeof fbuf, fmt); 132 do_percentm (fbuf, sizeof fbuf, fmt);
129 133
130 /* %Audit% This is log output. %2004.06.17,Safe% 134 /* %Audit% This is log output. %2004.06.17,Safe%
131 * If we truncate we hope the user can get a hint from the log. 135 * If we truncate we hope the user can get a hint from the log.
132 */ 136 */
133 va_start (list, fmt); 137 va_start (list, fmt);
134 vsnprintf (mbuf, sizeof mbuf, fbuf, list); 138 vsnprintf (mbuf, sizeof mbuf, fbuf, list);
135 va_end (list); 139 va_end (list);
136 140
137#ifndef DEBUG 141#ifndef DEBUG
138 syslog (LOG_INFO, "%s", mbuf); 142 syslog (LOG_INFO, "%s", mbuf);
@@ -140,26 +144,28 @@ int log_info (const char *fmt, ...) @@ -140,26 +144,28 @@ int log_info (const char *fmt, ...)
140 144
141 if (log_perror) { 145 if (log_perror) {
142 IGNORE_RET (write (STDERR_FILENO, mbuf, strlen (mbuf))); 146 IGNORE_RET (write (STDERR_FILENO, mbuf, strlen (mbuf)));
143 IGNORE_RET (write (STDERR_FILENO, "\n", 1)); 147 IGNORE_RET (write (STDERR_FILENO, "\n", 1));
144 } 148 }
145 149
146 return 0; 150 return 0;
147} 151}
148 152
149/* Log a debug message... */ 153/* Log a debug message... */
150 154
151int log_debug (const char *fmt, ...) 155int log_debug (const char *fmt, ...)
152{ 156{
 157 char mbuf [CVT_BUF_MAX + 1];
 158 char fbuf [CVT_BUF_MAX + 1];
153 va_list list; 159 va_list list;
154 160
155 do_percentm (fbuf, sizeof fbuf, fmt); 161 do_percentm (fbuf, sizeof fbuf, fmt);
156 162
157 /* %Audit% This is log output. %2004.06.17,Safe% 163 /* %Audit% This is log output. %2004.06.17,Safe%
158 * If we truncate we hope the user can get a hint from the log. 164 * If we truncate we hope the user can get a hint from the log.
159 */ 165 */
160 va_start (list, fmt); 166 va_start (list, fmt);
161 vsnprintf (mbuf, sizeof mbuf, fbuf, list); 167 vsnprintf (mbuf, sizeof mbuf, fbuf, list);
162 va_end (list); 168 va_end (list);
163 169
164#ifndef DEBUG 170#ifndef DEBUG
165 syslog (LOG_DEBUG, "%s", mbuf); 171 syslog (LOG_DEBUG, "%s", mbuf);