]> git.proxmox.com Git - grub2.git/blame - util/grub-menulst2cfg.c
fs/ntfs: Fix various OOB reads and writes (CVE-2023-4692, CVE-2023-4693)
[grub2.git] / util / grub-menulst2cfg.c
CommitLineData
fff175c7
VS
1/*
2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2010 Free Software Foundation, Inc.
4 *
5 * GRUB is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * GRUB is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
17 */
18
1d12cf29
YB
19#include <config.h>
20
fff175c7
VS
21#include <grub/legacy_parse.h>
22#include <stdio.h>
23#include <string.h>
24#include <errno.h>
e64334df 25#include <grub/util/misc.h>
c2fa6cbb 26#include <grub/misc.h>
6e0632e2 27#include <grub/i18n.h>
fff175c7
VS
28
29int
30main (int argc, char **argv)
31{
32 FILE *in, *out;
33 char *entryname = NULL;
34 char *buf = NULL;
35 size_t bufsize = 0;
6c6850ae
VS
36 char *suffix = xstrdup ("");
37 int suffixlen = 0;
8aeb1837 38 const char *out_fname = 0;
fff175c7 39
ae5540d3
VS
40 grub_util_host_init (&argc, &argv);
41
fff175c7
VS
42 if (argc >= 2 && argv[1][0] == '-')
43 {
6e0632e2 44 fprintf (stdout, _("Usage: %s [INFILE [OUTFILE]]\n"), argv[0]);
fff175c7
VS
45 return 0;
46 }
47
48 if (argc >= 2)
49 {
bb338aaf 50 in = grub_util_fopen (argv[1], "r");
fff175c7
VS
51 if (!in)
52 {
0ae70393 53 fprintf (stderr, _("cannot open `%s': %s"),
fff175c7
VS
54 argv[1], strerror (errno));
55 return 1;
56 }
57 }
58 else
59 in = stdin;
60
61 if (argc >= 3)
62 {
bb338aaf 63 out = grub_util_fopen (argv[2], "w");
fff175c7
VS
64 if (!out)
65 {
66 if (in != stdin)
67 fclose (in);
0ae70393 68 fprintf (stderr, _("cannot open `%s': %s"),
fff175c7
VS
69 argv[2], strerror (errno));
70 return 1;
71 }
8aeb1837 72 out_fname = argv[2];
fff175c7
VS
73 }
74 else
75 out = stdout;
76
77 while (1)
78 {
79 char *parsed;
80
81 if (getline (&buf, &bufsize, in) < 0)
82 break;
83
84 {
85 char *oldname = NULL;
e64334df 86 char *newsuffix;
fff175c7
VS
87
88 oldname = entryname;
9fdb2d7b 89 parsed = grub_legacy_parse (buf, &entryname, &newsuffix);
e64334df 90 if (newsuffix)
6c6850ae 91 {
e64334df 92 suffixlen += strlen (newsuffix);
6c6850ae 93 suffix = xrealloc (suffix, suffixlen + 1);
e64334df 94 strcat (suffix, newsuffix);
6c6850ae 95 }
fff175c7
VS
96 if (oldname != entryname && oldname)
97 fprintf (out, "}\n\n");
98 if (oldname != entryname)
64ad6157
VS
99 {
100 char *escaped = grub_legacy_escape (entryname, strlen (entryname));
101 fprintf (out, "menuentry \'%s\' {\n", escaped);
e64334df
VS
102 free (escaped);
103 free (oldname);
64ad6157 104 }
fff175c7
VS
105 }
106
107 if (parsed)
108 fprintf (out, "%s%s", entryname ? " " : "", parsed);
e64334df 109 free (parsed);
64ad6157 110 parsed = NULL;
fff175c7
VS
111 }
112
113 if (entryname)
114 fprintf (out, "}\n\n");
115
8aeb1837
VS
116 if (fwrite (suffix, 1, suffixlen, out) != suffixlen)
117 {
118 if (out_fname)
119 grub_util_error ("cannot write to `%s': %s",
120 out_fname, strerror (errno));
121 else
122 grub_util_error ("cannot write to the stdout: %s", strerror (errno));
123 }
fff175c7 124
e64334df
VS
125 free (buf);
126 free (suffix);
127 free (entryname);
64ad6157 128
fff175c7
VS
129 if (in != stdin)
130 fclose (in);
131 if (out != stdout)
132 fclose (out);
133
134 return 0;
135}