Return error on fopen(NULL)

https://sqlite.org/forum/forumpost/d1c96a9032e564f8
This commit is contained in:
Justine Tunney 2023-01-08 15:17:44 -08:00
parent 7ab4630cc9
commit 2bad436abf
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
2 changed files with 13 additions and 9 deletions

View file

@ -58,6 +58,10 @@ FILE *fopen(const char *pathname, const char *mode) {
FILE *f = 0; FILE *f = 0;
bool noclose; bool noclose;
int fd, flags; int fd, flags;
if (!pathname) {
efault();
return 0;
}
flags = fopenflags(mode); flags = fopenflags(mode);
pathname = fixpathname(pathname, flags); pathname = fixpathname(pathname, flags);
if ((fd = openpathname(pathname, flags, &noclose)) != -1) { if ((fd = openpathname(pathname, flags, &noclose)) != -1) {

View file

@ -116,7 +116,7 @@ static bool IsSupportedPath(const char *path) {
case '\r': case '\r':
case '\n': case '\n':
case '\\': case '\\':
Write(2, PROG, ": ", path, ": unsupported path\n", 0); Write(2, PROG, ": ", path, ": unsupported path\n", NULL);
return false; return false;
default: default:
break; break;
@ -134,7 +134,7 @@ static bool GetDigest(const char *path, FILE *f, unsigned char digest[32]) {
_unassert(!mbedtls_sha256_update_ret(&ctx, buf, got)); _unassert(!mbedtls_sha256_update_ret(&ctx, buf, got));
} }
if (ferror(f)) { if (ferror(f)) {
Write(2, PROG, ": ", path, ": ", _strerdoc(errno), "\n", 0); Write(2, PROG, ": ", path, ": ", _strerdoc(errno), "\n", NULL);
return false; return false;
} }
_unassert(!mbedtls_sha256_finish_ret(&ctx, digest)); _unassert(!mbedtls_sha256_finish_ret(&ctx, digest));
@ -149,7 +149,7 @@ static bool ProduceDigest(const char *path, FILE *f) {
if (!IsSupportedPath(path)) return false; if (!IsSupportedPath(path)) return false;
if (!GetDigest(path, f, digest)) return false; if (!GetDigest(path, f, digest)) return false;
hexpcpy(hexdigest, digest, 32); hexpcpy(hexdigest, digest, 32);
Write(1, hexdigest, " ", mode, path, "\n", 0); Write(1, hexdigest, " ", mode, path, "\n", NULL);
return true; return true;
} }
@ -181,13 +181,13 @@ static bool CheckDigests(const char *path, FILE *f) {
++g_mismatches; ++g_mismatches;
k = false; k = false;
} }
Write(1, path2, ": ", status, "\n", 0); Write(1, path2, ": ", status, "\n", NULL);
} else { } else {
k = false; k = false;
} }
fclose(f2); fclose(f2);
} else { } else {
Write(2, PROG, ": ", path2, ": ", _strerdoc(errno), "\n", 0); Write(2, PROG, ": ", path2, ": ", _strerdoc(errno), "\n", NULL);
k = false; k = false;
} }
continue; continue;
@ -196,11 +196,11 @@ static bool CheckDigests(const char *path, FILE *f) {
char linestr[12]; char linestr[12];
FormatInt32(linestr, line + 1); FormatInt32(linestr, line + 1);
Write(2, PROG, ": ", path, ":", linestr, ": ", Write(2, PROG, ": ", path, ":", linestr, ": ",
"improperly formatted checksum line", "\n", 0); "improperly formatted checksum line", "\n", NULL);
} }
} }
if (ferror(f)) { if (ferror(f)) {
Write(2, PROG, ": ", path, ": ", _strerdoc(errno), "\n", 0); Write(2, PROG, ": ", path, ": ", _strerdoc(errno), "\n", NULL);
k = false; k = false;
} }
return k; return k;
@ -228,7 +228,7 @@ int main(int argc, char *argv[]) {
k &= Process(argv[i], f); k &= Process(argv[i], f);
fclose(f); fclose(f);
} else { } else {
Write(2, PROG, ": ", argv[i], ": ", _strerdoc(errno), "\n", 0); Write(2, PROG, ": ", argv[i], ": ", _strerdoc(errno), "\n", NULL);
k = false; k = false;
} }
} }
@ -237,7 +237,7 @@ int main(int argc, char *argv[]) {
char ibuf[12]; char ibuf[12];
FormatInt32(ibuf, g_mismatches); FormatInt32(ibuf, g_mismatches);
Write(2, PROG, ": WARNING: ", ibuf, " computed checksum did NOT match\n", Write(2, PROG, ": WARNING: ", ibuf, " computed checksum did NOT match\n",
0); NULL);
} }
return !k; return !k;
} }