Skip to content

Commit 733322f

Browse files
committed
uksmctl: add more error checking
1 parent 56c3b5f commit 733322f

File tree

1 file changed

+46
-7
lines changed

1 file changed

+46
-7
lines changed

uksmctl/uksmctl.c

+46-7
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <sys/stat.h>
2323
#include <sys/types.h>
2424
#include <sysexits.h>
25+
#include <errno.h>
2526

2627
#define UKSMDIR "/sys/kernel/mm/uksm"
2728
#define UKSMRUN UKSMDIR"/run"
@@ -103,8 +104,17 @@ int main(int argc, char **argv)
103104
fprintf(stderr, "Unable to open run file\n");
104105
exit(EX_OSFILE);
105106
}
106-
fprintf(f, "%d", 1);
107-
fclose(f);
107+
if (fprintf(f, "%d", 1) < 0)
108+
{
109+
fprintf(stderr, "Unable to write to run file\n");
110+
fclose(f);
111+
exit(EX_OSFILE);
112+
}
113+
if (0 != fclose(f))
114+
{
115+
fprintf(stderr, "Unable to close run file\n");
116+
exit(EX_OSFILE);
117+
}
108118

109119
switch (verbose)
110120
{
@@ -127,8 +137,17 @@ int main(int argc, char **argv)
127137
fprintf(stderr, "Unable to open run file\n");
128138
exit(EX_OSFILE);
129139
}
130-
fprintf(f, "%d", 0);
131-
fclose(f);
140+
if (fprintf(f, "%d", 0) < 0)
141+
{
142+
fprintf(stderr, "Unable to write to run file\n");
143+
fclose(f);
144+
exit(EX_OSFILE);
145+
}
146+
if (0 != fclose(f))
147+
{
148+
fprintf(stderr, "Unable to close run file\n");
149+
exit(EX_OSFILE);
150+
}
132151

133152
switch (verbose)
134153
{
@@ -151,8 +170,19 @@ int main(int argc, char **argv)
151170
exit(EX_OSFILE);
152171
}
153172
unsigned int run = 0;
173+
errno = 0;
154174
fscanf(f, "%d", &run);
155-
fclose(f);
175+
if (0 != errno)
176+
{
177+
fprintf(stderr, "Unable to read run file\n");
178+
fclose(f);
179+
exit(EX_OSFILE);
180+
}
181+
if (0 != fclose(f))
182+
{
183+
fprintf(stderr, "Unable to close run file\n");
184+
exit(EX_OSFILE);
185+
}
156186

157187
switch (run)
158188
{
@@ -182,8 +212,17 @@ int main(int argc, char **argv)
182212
fprintf(stderr, "Unable to open run file\n");
183213
exit(EX_OSFILE);
184214
}
185-
fprintf(f, "%d", run);
186-
fclose(f);
215+
if (fprintf(f, "%d", run) < 0)
216+
{
217+
fprintf(stderr, "Unable to write to run file\n");
218+
fclose(f);
219+
exit(EX_OSFILE);
220+
}
221+
if (0 != fclose(f))
222+
{
223+
fprintf(stderr, "Unable to close run file\n");
224+
exit(EX_OSFILE);
225+
}
187226

188227
switch (run)
189228
{

0 commit comments

Comments
 (0)