fopen/fclose file descriptor leak
I thought I'd make an issue about this bug, I hope that's fine.
First up I am using a fairly outdated kernel, sys version
:
Commit: f3bee175-dirty
Build time: 2019-11-12T18:01+01:00
Commit f3bee175
is
f3bee17 (9 months ago) Fix memory leak when opendir fails.
So this issue might be solved already, but the commits since then do not (obviously) indicate that this bug was solved, and it seems to be an issue with the libraries, not the kernel anyway. (Can't update the kernel currently because of the mini jtag connector instead of a normal jtag connector that the OBCv2 uses)
Minimal reproducing example:
#include <stdio.h>
#include <timing.h>
int main (int argc, char **argv)
{
printf ("Hello, World!\n");
char data[200];
while(1)
{
FILE* stream = fopen("/logs/hkmsg", "r");
fread(data, 1, 200, stream);
data[32] = 0;
printf("Data: %s\n", data);
fclose(stream);
delay_ms(5000); // some other non Orbit related sleep function should work as well
}
return 0;
}
Just a modified hello world that opens, reads, prints part of it, and closes a text file.
The problem, when running this helloworld &
and continuously inspecting /proc/PID/fd
$ cat proc/17/fd
0 0 /dev tty1
1 0 /dev tty1
2 7 /
3 8 /
later,
$ cat proc/17/fd
0 0 /dev tty1
1 0 /dev tty1
2 7 /
3 8 /
4 9 /
5 10 /
6 11 /
7 12 /
More and more file descriptors get allocated, until a Data Abort
crash. The crash can be caught if the pointer returned by fopen()
is checked for being NULL
and handling that before trying to read from it. No errno
is set in this case.
Replacing the fopen()
, fread()
, and fclose()
functions with equivalent open()
, read()
, and close()
calls, does not cause this bug, so that is a workaround.