Thu May 27 13:36:33 2021 UTC ()
Don't use the stack, print to the buffer directly (this was one of the
biggest stack users).


(christos)
diff -r1.6 -r1.7 src/sys/net/dl_print.c

cvs diff -r1.6 -r1.7 src/sys/net/dl_print.c (expand / switch to unified diff)

--- src/sys/net/dl_print.c 2019/04/30 20:56:32 1.6
+++ src/sys/net/dl_print.c 2021/05/27 13:36:33 1.7
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: dl_print.c,v 1.6 2019/04/30 20:56:32 kre Exp $ */ 1/* $NetBSD: dl_print.c,v 1.7 2021/05/27 13:36:33 christos Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 2014 The NetBSD Foundation, Inc. 4 * Copyright (c) 2014 The NetBSD Foundation, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
@@ -19,72 +19,100 @@ @@ -19,72 +19,100 @@
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE. 26 * POSSIBILITY OF SUCH DAMAGE.
27 */ 27 */
28#include <sys/cdefs.h> 28#include <sys/cdefs.h>
29#include <sys/types.h> 29#include <sys/types.h>
30 30
31#ifdef _KERNEL 31#ifdef _KERNEL
32__KERNEL_RCSID(0, "$NetBSD: dl_print.c,v 1.6 2019/04/30 20:56:32 kre Exp $"); 32__KERNEL_RCSID(0, "$NetBSD: dl_print.c,v 1.7 2021/05/27 13:36:33 christos Exp $");
33#include <sys/systm.h> 33#include <sys/systm.h>
34#else 34#else
35__RCSID("$NetBSD: dl_print.c,v 1.6 2019/04/30 20:56:32 kre Exp $"); 35__RCSID("$NetBSD: dl_print.c,v 1.7 2021/05/27 13:36:33 christos Exp $");
36#include <stdio.h> 36#include <stdio.h>
37static const char hexdigits[] = "0123456789abcdef"; 37static const char hexdigits[] = "0123456789abcdef";
38#endif 38#endif
39#include <net/if_dl.h> 39#include <net/if_dl.h>
40 40
41char * 41static int
42lla_snprintf(char *dst, size_t dst_len, const void *src, size_t src_len) 42lla_snprintf1(char *dst, size_t dst_len, const void *src, size_t src_len)
43{ 43{
44 char *dp; 44 char *dp;
45 const uint8_t *sp, *ep; 45 const uint8_t *sp, *ep;
46 46
47 if (src_len == 0 || dst_len < 3) { 47 if (src_len == 0 || dst_len < 3) {
48 if (dst_len != 0) 48 if (dst_len != 0)
49 dst[0] = '\0'; 49 dst[0] = '\0';
50 return NULL; 50 return src_len ? (int)(src_len * 3) - 1 : 0;
51 } 51 }
52 52
53 dp = dst; 53 dp = dst;
54 sp = (const uint8_t *)src; 54 sp = (const uint8_t *)src;
55 ep = sp + src_len; 55 ep = sp + src_len;
56 while (sp < ep) { 56 while (sp < ep) {
57 if (dst_len < 3) 57 if (dst_len-- == 0)
58 break; 58 break;
59 dst_len -= 3; 
60 *dp++ = hexdigits[(*sp) >> 4]; 59 *dp++ = hexdigits[(*sp) >> 4];
 60 if (dst_len-- == 0)
 61 break;
61 *dp++ = hexdigits[(*sp++) & 0xf]; 62 *dp++ = hexdigits[(*sp++) & 0xf];
 63 if (dst_len-- == 0)
 64 break;
62 *dp++ = ':'; 65 *dp++ = ':';
63 } 66 }
64 *--dp = 0; 67 *--dp = '\0';
 68
 69 return (int)(src_len * 3) - 1;
 70}
65 71
 72char *
 73lla_snprintf(char *dst, size_t dst_len, const void *src, size_t src_len)
 74{
 75 if (lla_snprintf1(dst, dst_len, src, src_len) == -1)
 76 return NULL;
66 return dst; 77 return dst;
67} 78}
68 79
 80#define clip(a, b) ((a) > (size_t)(b) ? (a) - (size_t)(b) : 0)
 81
69int 82int
70dl_print(char *buf, size_t len, const struct dl_addr *dl) 83dl_print(char *buf, size_t len, const struct dl_addr *dl)
71{ 84{
72 char abuf[256 * 3]; 85 int l = snprintf(buf, len, "%.*s/%hhu#",
 86 (int)dl->dl_nlen, dl->dl_data, dl->dl_type);
 87 if (l == -1)
 88 return l;
 89 int ll = lla_snprintf1(buf + l, clip(len, l),
 90 dl->dl_data + dl->dl_nlen, dl->dl_alen);
 91 if (ll == -1)
 92 return ll;
73 93
74 lla_snprintf(abuf, sizeof(abuf), dl->dl_data+dl->dl_nlen, dl->dl_alen); 94 return ll + l;
75 return snprintf(buf, len, "%.*s/%hhu#%s", 
76 (int)dl->dl_nlen, dl->dl_data, dl->dl_type, abuf); 
77} 95}
78 96
79int 97int
80sdl_print(char *buf, size_t len, const void *v) 98sdl_print(char *buf, size_t len, const void *v)
81{ 99{
82 const struct sockaddr_dl *sdl = v; 100 const struct sockaddr_dl *sdl = v;
83 char abuf[LINK_ADDRSTRLEN]; 
84 101
85 if (sdl->sdl_slen == 0 && sdl->sdl_nlen == 0 && sdl->sdl_alen == 0) 102 if (sdl->sdl_slen == 0 && sdl->sdl_nlen == 0 && sdl->sdl_alen == 0)
86 return snprintf(buf, len, "link#%hu", sdl->sdl_index); 103 return snprintf(buf, len, "link#%hu", sdl->sdl_index);
87 104
88 dl_print(abuf, sizeof(abuf), &sdl->sdl_addr); 105 if (len > 0) {
89 return snprintf(buf, len, "[%s]:%hu", abuf, sdl->sdl_index); 106 buf[0] = '[';
 107 len--;
 108 }
 109 int l = dl_print(buf + 1, len, &sdl->sdl_addr);
 110 if (l == -1)
 111 return l;
 112 l++;
 113 len++;
 114 int ll = snprintf(buf + l, clip(len, l), "]:%hu", sdl->sdl_index);
 115 if (ll == -1)
 116 return ll;
 117 return ll + l;
90} 118}