]> git.proxmox.com Git - efi-boot-shim.git/blob - errlog.c
VLogError(): Avoid NULL pointer dereferences in (V)Sprint calls
[efi-boot-shim.git] / errlog.c
1 /*
2 * errlog.c
3 * Copyright 2017 Peter Jones <pjones@redhat.com>
4 *
5 * Distributed under terms of the GPLv3 license.
6 */
7
8 #include "shim.h"
9
10 static CHAR16 **errs = NULL;
11 static UINTN nerrs = 0;
12
13 EFI_STATUS
14 VLogError(const char *file, int line, const char *func, CHAR16 *fmt, va_list args)
15 {
16 va_list args2;
17 CHAR16 **newerrs;
18
19 newerrs = ReallocatePool(errs, (nerrs + 1) * sizeof(*errs),
20 (nerrs + 3) * sizeof(*errs));
21 if (!newerrs)
22 return EFI_OUT_OF_RESOURCES;
23
24 newerrs[nerrs] = PoolPrint(L"%a:%d %a() ", file, line, func);
25 if (!newerrs[nerrs])
26 return EFI_OUT_OF_RESOURCES;
27 va_copy(args2, args);
28 newerrs[nerrs+1] = VPoolPrint(fmt, args2);
29 if (!newerrs[nerrs+1])
30 return EFI_OUT_OF_RESOURCES;
31 va_end(args2);
32
33 nerrs += 2;
34 newerrs[nerrs] = NULL;
35 errs = newerrs;
36
37 return EFI_SUCCESS;
38 }
39
40 EFI_STATUS
41 LogError_(const char *file, int line, const char *func, CHAR16 *fmt, ...)
42 {
43 va_list args;
44 EFI_STATUS efi_status;
45
46 va_start(args, fmt);
47 efi_status = VLogError(file, line, func, fmt, args);
48 va_end(args);
49
50 return efi_status;
51 }
52
53 VOID
54 PrintErrors(VOID)
55 {
56 UINTN i;
57
58 if (!verbose)
59 return;
60
61 for (i = 0; i < nerrs; i++)
62 console_print(L"%s", errs[i]);
63 }
64
65 VOID
66 ClearErrors(VOID)
67 {
68 UINTN i;
69
70 for (i = 0; i < nerrs; i++)
71 FreePool(errs[i]);
72 FreePool(errs);
73 nerrs = 0;
74 errs = NULL;
75 }
76
77 // vim:fenc=utf-8:tw=75