fclose: restore errno after call to free() if close() fails
This commit is contained in:
parent
93f6be9319
commit
4f2b3ce5d1
@ -1,3 +1,4 @@
|
|||||||
|
#include <errno.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <luna.h>
|
#include <luna.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
@ -9,9 +10,15 @@ extern "C"
|
|||||||
int fclose(FILE* stream)
|
int fclose(FILE* stream)
|
||||||
{
|
{
|
||||||
int status = close(stream->fd);
|
int status = close(stream->fd);
|
||||||
free(stream); // We do not want to leak memory. man fclose(3) says that whether fclose() fails or not, any
|
if (status < 0)
|
||||||
// further operation on the stream results in undefined behavior. So we are free to free the
|
{
|
||||||
// stream.
|
int savederr = errno;
|
||||||
|
free(stream); // We do not want to leak memory. man fclose(3) says that whether fclose() fails or not, any
|
||||||
|
// further operation on the stream results in undefined behavior. So we are free to free the
|
||||||
|
// stream.
|
||||||
|
errno = savederr; // free might reset errno. We don't want that.
|
||||||
|
}
|
||||||
|
else { free(stream); }
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user