Fix some static analysis issues

This commit is contained in:
Justine Tunney 2024-07-27 09:16:54 -07:00
parent fb54604b31
commit f147d3dde9
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
30 changed files with 375 additions and 266 deletions

View file

@ -16,6 +16,7 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/assert.h"
#include "libc/calls/calls.h"
#include "libc/stdio/internal.h"
#include "libc/stdio/stdio.h"
@ -37,8 +38,8 @@
* @return stream object if successful, or NULL w/ errno
*/
FILE *freopen(const char *pathname, const char *mode, FILE *stream) {
int fd;
FILE *res;
int fd, fd2;
unsigned flags;
flags = fopenflags(mode);
flockfile(stream);
@ -46,19 +47,30 @@ FILE *freopen(const char *pathname, const char *mode, FILE *stream) {
if (pathname) {
/* open new stream, overwriting existing alloc */
if ((fd = open(pathname, flags, 0666)) != -1) {
dup3(fd, stream->fd, flags & O_CLOEXEC);
fd2 = dup3(fd, stream->fd, flags & O_CLOEXEC);
close(fd);
stream->iomode = flags;
stream->beg = 0;
stream->end = 0;
res = stream;
if (fd2 != -1) {
stream->fd = fd2;
stream->iomode = flags;
stream->beg = 0;
stream->end = 0;
res = stream;
} else {
res = NULL;
}
} else {
res = NULL;
}
} else {
fcntl(stream->fd, F_SETFD, !!(flags & O_CLOEXEC));
fcntl(stream->fd, F_SETFL, flags & ~O_CLOEXEC);
res = stream;
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wanalyzer-fd-use-without-check"
if (fcntl(stream->fd, F_SETFD, !!(flags & O_CLOEXEC)) != -1 &&
fcntl(stream->fd, F_SETFL, flags & ~O_CLOEXEC) != -1) {
#pragma GCC diagnostic pop
res = stream;
} else {
res = NULL;
}
}
funlockfile(stream);
return res;