ccd82fd057bfa5925828dad4798bea274dc29969
[openwrt/openwrt.git] / package / utils / util-linux / patches / 0002-lib-colors-use-static-buffers-when-parse-scheme.patch
1 From 6508db29ded734ac4ff5e5e19486c143c9eb3d89 Mon Sep 17 00:00:00 2001
2 From: Karel Zak <kzak@redhat.com>
3 Date: Fri, 21 Nov 2014 12:23:47 +0100
4 Subject: [PATCH] lib/colors: use static buffers when parse scheme
5
6 * use static buffers when parse scheme colors
7 * cleanup deallocation on error in sequence parser
8
9 Signed-off-by: Karel Zak <kzak@redhat.com>
10 ---
11
12 This is modified upstream patch.
13
14 --- a/lib/colors.c
15 +++ b/lib/colors.c
16 @@ -416,28 +416,31 @@ static int colors_add_scheme(struct ul_c
17 char *name,
18 char *seq0)
19 {
20 - struct ul_color_scheme *cs;
21 - char *seq;
22 + struct ul_color_scheme *cs = NULL;
23 + char *seq = NULL;
24 int rc;
25
26 if (!cc || !name || !*name || !seq0 || !*seq0)
27 return -EINVAL;
28
29 rc = cn_sequence(seq0, &seq);
30 - free(seq0);
31 if (rc)
32 return rc;
33
34 + rc = -ENOMEM;
35 +
36 /* convert logical name (e.g. "red") to real ESC code */
37 if (isalpha(*seq)) {
38 const char *s = color_sequence_from_colorname(seq);
39 char *p;
40
41 - if (!s)
42 - return -EINVAL;
43 + if (!s) {
44 + rc = -EINVAL;
45 + goto err;
46 + }
47 p = strdup(s);
48 if (!p)
49 - return -ENOMEM;
50 + goto err;
51 free(seq);
52 seq = p;
53 }
54 @@ -447,17 +450,28 @@ static int colors_add_scheme(struct ul_c
55 void *tmp = realloc(cc->schemes, (cc->nschemes + 10)
56 * sizeof(struct ul_color_scheme));
57 if (!tmp)
58 - return -ENOMEM;
59 + goto err;
60 cc->schemes = tmp;
61 cc->schemes_sz = cc->nschemes + 10;
62 }
63
64 /* add a new item */
65 - cs = &cc->schemes[cc->nschemes++];
66 - cs->name = name;
67 + cs = &cc->schemes[cc->nschemes];
68 cs->seq = seq;
69 + cs->name = strdup(name);
70 + if (!cs->name)
71 + goto err;
72
73 + cc->nschemes++;
74 return 0;
75 +err:
76 + if (cs) {
77 + free(cs->seq);
78 + free(cs->name);
79 + cs->seq = cs->name = NULL;
80 + } else
81 + free(seq);
82 + return rc;
83 }
84
85 /*
86 @@ -543,7 +557,8 @@ static int colors_read_schemes(struct ul
87 {
88 int rc = 0;
89 FILE *f = NULL;
90 - char buf[BUFSIZ];
91 + char buf[BUFSIZ],
92 + cn[129], seq[129];
93
94 if (!cc->configured)
95 rc = colors_read_configuration(cc);
96 @@ -560,7 +575,6 @@ static int colors_read_schemes(struct ul
97 }
98
99 while (fgets(buf, sizeof(buf), f)) {
100 - char *cn = NULL, *seq = NULL;
101 char *p = strchr(buf, '\n');
102
103 if (!p) {
104 @@ -576,17 +590,14 @@ static int colors_read_schemes(struct ul
105 if (*p == '\0' || *p == '#')
106 continue;
107
108 - rc = sscanf(p, UL_SCNsA" " /* name */
109 - UL_SCNsA, /* color */
110 - &cn, &seq);
111 - if (rc == 2 && cn && seq)
112 + rc = sscanf(p, "%128[^ ] %128[^\n ]", cn, seq);
113 + if (rc == 2 && *cn && *seq) {
114 rc = colors_add_scheme(cc, cn, seq); /* set rc=0 on success */
115 - if (rc) {
116 - free(cn);
117 - free(seq);
118 + if (rc)
119 + goto done;
120 }
121 - rc = 0;
122 }
123 + rc = 0;
124
125 done:
126 if (f)