krb5: Fix SQLite3 ccache bugs/warnings

This commit is contained in:
Nicolas Williams
2021-12-02 21:23:52 -06:00
parent cb751670dd
commit 82a8744787

View File

@@ -276,9 +276,9 @@ make_dir(krb5_context context, const char *name)
static krb5_error_code static krb5_error_code
default_db(krb5_context context, const char *name, sqlite3 **db, char **file) default_db(krb5_context context, const char *name, sqlite3 **db, char **file)
{ {
krb5_error_code ret = 0;
char *s = NULL; char *s = NULL;
char *f = NULL; char *f = NULL;
int ret;
if (file) if (file)
*file = NULL; *file = NULL;
@@ -315,13 +315,24 @@ default_db(krb5_context context, const char *name, sqlite3 **db, char **file)
#endif #endif
ret = make_dir(context, f); ret = make_dir(context, f);
if (ret == 0) if (ret == 0) {
ret = sqlite3_open_v2(f, db, SQLITE_OPEN_READWRITE, NULL); int sret;
if (ret != SQLITE_OK) {
sqlite3_close(*db); sret = sqlite3_open_v2(f, db, SQLITE_OPEN_READWRITE, NULL);
krb5_clear_error_message(context); if (sret != SQLITE_OK) {
free(f); if (*db) {
return ENOENT; krb5_set_error_message(context, ENOENT,
N_("Error opening scache file %s: %s (%d)", ""),
f, sqlite3_errmsg(*db), sret);
sqlite3_close(*db);
*db = NULL;
} else
krb5_set_error_message(context, ENOENT,
N_("Error opening scache file %s: %s (%d)", ""),
f, sqlite3_errstr(sret), sret);
free(f);
return ENOENT;
}
} }
#ifndef WIN32 #ifndef WIN32
@@ -341,7 +352,7 @@ default_db(krb5_context context, const char *name, sqlite3 **db, char **file)
sqlite3_trace(*db, trace, NULL); sqlite3_trace(*db, trace, NULL);
#endif #endif
return 0; return ret;
} }
static krb5_error_code static krb5_error_code
@@ -480,8 +491,9 @@ scc_alloc(krb5_context context,
static krb5_error_code static krb5_error_code
open_database(krb5_context context, krb5_scache *s, int flags) open_database(krb5_context context, krb5_scache *s, int flags)
{ {
krb5_error_code ret;
struct stat st; struct stat st;
int ret; int sret;
if (!(flags & SQLITE_OPEN_CREATE) && stat(s->file, &st) == 0 && if (!(flags & SQLITE_OPEN_CREATE) && stat(s->file, &st) == 0 &&
@@ -489,18 +501,20 @@ open_database(krb5_context context, krb5_scache *s, int flags)
return ENOENT; return ENOENT;
ret = make_dir(context, s->file); ret = make_dir(context, s->file);
if (ret == 0) if (ret)
ret = sqlite3_open_v2(s->file, &s->db, SQLITE_OPEN_READWRITE|flags, NULL); return ret;
if (ret) { sret = sqlite3_open_v2(s->file, &s->db, SQLITE_OPEN_READWRITE|flags, NULL);
if (sret != SQLITE_OK) {
if (s->db) { if (s->db) {
krb5_set_error_message(context, ENOENT, krb5_set_error_message(context, ENOENT,
N_("Error opening scache file %s: %s", ""), N_("Error opening scache file %s: %s (%d)", ""),
s->file, sqlite3_errmsg(s->db)); s->file, sqlite3_errmsg(s->db), sret);
sqlite3_close(s->db); sqlite3_close(s->db);
s->db = NULL; s->db = NULL;
} else } else
krb5_set_error_message(context, ENOENT, krb5_set_error_message(context, ENOENT,
N_("malloc: out of memory", "")); N_("Error opening scache file %s: %s (%d)", ""),
s->file, sqlite3_errstr(sret), sret);
return ENOENT; return ENOENT;
} }
return 0; return 0;