Skip to content

Commit a40612e

Browse files
author
Al Viro
committed
genheaders: %-<width>s had been there since v6; %-*s - since v7
Please, use at least K&R C; printf had been able to left-adjust a field for as long as stdio existed and use of '*' for variable width had been there since v7. Yes, the first edition of K&R didn't cover the latter feature (it slightly predates v7), but you are using a much later feature of the language than that - in K&R C static char *stoupperx(const char *s) { ... } would've been spelled as static char *stoupperx(s) char *s; { ... } While we are at it, the use of strstr() is bogus - it finds the _first_ instance of substring, so it's a lousy fit for checking if a string ends with given suffix... Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
1 parent 6510223 commit a40612e

1 file changed

Lines changed: 9 additions & 20 deletions

File tree

‎scripts/selinux/genheaders/genheaders.c‎

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ struct security_class_mapping {
1919
#include "classmap.h"
2020
#include "initial_sid_to_string.h"
2121

22-
#define max(x, y) (((int)(x) > (int)(y)) ? x : y)
23-
2422
const char *progname;
2523

2624
static void usage(void)
@@ -46,11 +44,9 @@ static char *stoupperx(const char *s)
4644

4745
int main(int argc, char *argv[])
4846
{
49-
int i, j, k;
47+
int i, j;
5048
int isids_len;
5149
FILE *fout;
52-
const char *needle = "SOCKET";
53-
char *substr;
5450

5551
progname = argv[0];
5652

@@ -80,30 +76,25 @@ int main(int argc, char *argv[])
8076

8177
for (i = 0; secclass_map[i].name; i++) {
8278
struct security_class_mapping *map = &secclass_map[i];
83-
fprintf(fout, "#define SECCLASS_%s", map->name);
84-
for (j = 0; j < max(1, 40 - strlen(map->name)); j++)
85-
fprintf(fout, " ");
86-
fprintf(fout, "%2d\n", i+1);
79+
fprintf(fout, "#define SECCLASS_%-39s %2d\n", map->name, i+1);
8780
}
8881

8982
fprintf(fout, "\n");
9083

9184
for (i = 1; i < isids_len; i++) {
9285
const char *s = initial_sid_to_string[i];
93-
fprintf(fout, "#define SECINITSID_%s", s);
94-
for (j = 0; j < max(1, 40 - strlen(s)); j++)
95-
fprintf(fout, " ");
96-
fprintf(fout, "%2d\n", i);
86+
fprintf(fout, "#define SECINITSID_%-39s %2d\n", s, i);
9787
}
9888
fprintf(fout, "\n#define SECINITSID_NUM %d\n", i-1);
9989
fprintf(fout, "\nstatic inline bool security_is_socket_class(u16 kern_tclass)\n");
10090
fprintf(fout, "{\n");
10191
fprintf(fout, "\tbool sock = false;\n\n");
10292
fprintf(fout, "\tswitch (kern_tclass) {\n");
10393
for (i = 0; secclass_map[i].name; i++) {
94+
static char s[] = "SOCKET";
10495
struct security_class_mapping *map = &secclass_map[i];
105-
substr = strstr(map->name, needle);
106-
if (substr && strcmp(substr, needle) == 0)
96+
int len = strlen(map->name), l = sizeof(s) - 1;
97+
if (len >= l && memcmp(map->name + len - l, s, l) == 0)
10798
fprintf(fout, "\tcase SECCLASS_%s:\n", map->name);
10899
}
109100
fprintf(fout, "\t\tsock = true;\n");
@@ -129,17 +120,15 @@ int main(int argc, char *argv[])
129120

130121
for (i = 0; secclass_map[i].name; i++) {
131122
struct security_class_mapping *map = &secclass_map[i];
123+
int len = strlen(map->name);
132124
for (j = 0; map->perms[j]; j++) {
133125
if (j >= 32) {
134126
fprintf(stderr, "Too many permissions to fit into an access vector at (%s, %s).\n",
135127
map->name, map->perms[j]);
136128
exit(5);
137129
}
138-
fprintf(fout, "#define %s__%s", map->name,
139-
map->perms[j]);
140-
for (k = 0; k < max(1, 40 - strlen(map->name) - strlen(map->perms[j])); k++)
141-
fprintf(fout, " ");
142-
fprintf(fout, "0x%08xU\n", (1<<j));
130+
fprintf(fout, "#define %s__%-*s 0x%08xU\n", map->name,
131+
39-len, map->perms[j], 1U<<j);
143132
}
144133
}
145134

0 commit comments

Comments
 (0)