Skip to content

Commit cb1f71b

Browse files
committed
Properly detect failure to open logs. Avoid trying to write on failure.
1 parent c4d757b commit cb1f71b

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

instruments/base/base.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@ static char logfile[] = "/data/local/tmp/adbi.log";
3636
static void logmsgtofile(char *msg)
3737
{
3838
int fp = open(logfile, O_WRONLY|O_APPEND);
39-
write(fp, msg, strlen(msg));
40-
close(fp);
39+
if (fp != -1) {
40+
write(fp, msg, strlen(msg));
41+
close(fp);
42+
}
4143
}
4244

4345
static void logmsgtostdout(char *msg)

instruments/example/epoll.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@
3232
#undef log
3333

3434
#define log(...) \
35-
{FILE *fp = fopen("/data/local/tmp/adbi_example.log", "a+");\
35+
{FILE *fp = fopen("/data/local/tmp/adbi_example.log", "a+"); if (fp) {\
3636
fprintf(fp, __VA_ARGS__);\
37-
fclose(fp);}
37+
fclose(fp);}}
3838

3939

4040
// this file is going to be compiled into a thumb mode binary

0 commit comments

Comments
 (0)