|
| 1 | +/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ |
| 2 | +│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ |
| 3 | +╞══════════════════════════════════════════════════════════════════════════════╡ |
| 4 | +│ Copyright 2021 Justine Alexandra Roberts Tunney │ |
| 5 | +│ │ |
| 6 | +│ Permission to use, copy, modify, and/or distribute this software for │ |
| 7 | +│ any purpose with or without fee is hereby granted, provided that the │ |
| 8 | +│ above copyright notice and this permission notice appear in all copies. │ |
| 9 | +│ │ |
| 10 | +│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ |
| 11 | +│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ |
| 12 | +│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ |
| 13 | +│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ |
| 14 | +│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ |
| 15 | +│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ |
| 16 | +│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ |
| 17 | +│ PERFORMANCE OF THIS SOFTWARE. │ |
| 18 | +╚─────────────────────────────────────────────────────────────────────────────*/ |
| 19 | +#include "libc/calls/groups.internal.h" |
| 20 | +#include "libc/dce.h" |
| 21 | +#include "libc/intrin/asan.internal.h" |
| 22 | +#include "libc/intrin/kprintf.h" |
| 23 | +#include "libc/intrin/popcnt.h" |
| 24 | +#include "libc/macros.internal.h" |
| 25 | +#include "libc/str/str.h" |
| 26 | + |
| 27 | +#define N 128 |
| 28 | + |
| 29 | +const char *(DescribeGidList)(char buf[N], int rc, int size, |
| 30 | + const uint32_t list[]) { |
| 31 | + if ((rc == -1) || (size < 0)) return "n/a"; |
| 32 | + if (!size) return "{}"; |
| 33 | + if (!list) return "NULL"; |
| 34 | + if ((!IsAsan() && kisdangerous(list)) || |
| 35 | + (IsAsan() && !__asan_is_valid(list, size * sizeof(list[0])))) { |
| 36 | + ksnprintf(buf, N, "%p", list); |
| 37 | + return buf; |
| 38 | + } |
| 39 | + int i = 0, n = N; |
| 40 | + i += ksnprintf(buf + i, MAX(0, n - i), "{"); |
| 41 | + unsigned c; |
| 42 | + for (c = 0; c < size && MAX(0, n - i) > 0; c++) { |
| 43 | + i += ksnprintf(buf + i, MAX(0, n - i), "%u, ", list[c]); |
| 44 | + } |
| 45 | + if (c == size) { |
| 46 | + if (buf[i - 1] == ' ') i--; |
| 47 | + if (buf[i - 1] == ',') i--; |
| 48 | + i += ksnprintf(buf + i, MAX(0, n - i), "}"); |
| 49 | + } |
| 50 | + return buf; |
| 51 | +} |
0 commit comments