jcs
/subtext
/amendments
/642
db: Minor bug fixes, memory leaks
jcs made amendment 642 1 day ago
--- db.c Sat Dec 6 18:06:42 2025
+++ db.c Tue Sep 22 14:30:47 2026
@@ -171,6 +171,7 @@ db_open(Str255 file, short vrefnum, bool ignore_last)
SFTypeList types;
StringHandle lastfileh;
struct db *ret;
+ size_t len;
if (file[0]) {
getpath(vrefnum, file, filepath, true);
@@ -182,7 +183,10 @@ db_open(Str255 file, short vrefnum, bool ignore_last)
if (!ignore_last && (lastfileh = GetString(STR_LAST_DB))) {
HLock(lastfileh);
- memcpy(filepath, *lastfileh, sizeof(filepath));
+ len = GetHandleSize((Handle)lastfileh);
+ if (len > sizeof(filepath))
+ len = sizeof(filepath);
+ memcpy(filepath, *lastfileh, len);
HUnlock(lastfileh);
ReleaseResource(lastfileh);
if (FStat(filepath, &sb) == 0) {
@@ -297,6 +301,7 @@ db_init(Str255 path, short vrefnum, struct bile *bile)
if (db_migrate(tdb, was_new, fullpath) != 0) {
bile_close(tdb->bile);
+ xfree(&tdb->bile);
xfree(&tdb);
return NULL;
}
@@ -355,16 +360,20 @@ db_close(struct db *tdb)
if (tdb->boards) {
for (n = 0; n < tdb->nboards; n++) {
- if (tdb->boards[n].bile)
+ if (tdb->boards[n].bile) {
bile_close(tdb->boards[n].bile);
+ xfree(&tdb->boards[n].bile);
+ }
}
xfree(&tdb->boards);
}
if (tdb->folders) {
for (n = 0; n < tdb->nfolders; n++) {
- if (tdb->folders[n].bile)
+ if (tdb->folders[n].bile) {
bile_close(tdb->folders[n].bile);
+ xfree(&tdb->folders[n].bile);
+ }
}
xfree(&tdb->folders);
}
@@ -475,6 +484,8 @@ db_migrate(struct db *tdb, short is_new, Str255 fullpa
for (n = 0; n < nids; n++) {
size = bile_read_alloc(sessions_bile, SL_LOG_RTYPE,
ids[n], &data);
+ if (data != NULL)
+ xfree(&data);
size += member_size(struct session_log, location);
if (bile_resize(sessions_bile, SL_LOG_RTYPE,
ids[n], size) != size)
@@ -482,8 +493,10 @@ db_migrate(struct db *tdb, short is_new, Str255 fullpa
}
bile_flush(sessions_bile, true);
- xfree(&ids);
+ if (ids != NULL)
+ xfree(&ids);
bile_close(sessions_bile);
+ xfree(&sessions_bile);
break;
}
case 19: {
@@ -622,7 +635,7 @@ db_config_load(struct db *tdb)
warn("db_config_load: read config of size %lu, but expected %lu",
rlen, sizeof(tdb->config));
- memcpy(&tdb->config, newconfig, sizeof(tdb->config));
+ memcpy(&tdb->config, newconfig, MIN(rlen, sizeof(tdb->config)));
xfree(&newconfig);
}
@@ -638,8 +651,10 @@ db_cache_boards(struct db *tdb)
if (tdb->boards) {
for (n = 0; n < tdb->nboards; n++) {
- if (tdb->boards[n].bile)
+ if (tdb->boards[n].bile) {
bile_close(tdb->boards[n].bile);
+ xfree(&tdb->boards[n].bile);
+ }
}
xfree(&tdb->boards);
}
@@ -773,6 +788,7 @@ db_board_delete(struct db *tdb, struct board *board)
return;
}
bile_close(board->bile);
+ xfree(&board->bile);
}
void
@@ -787,8 +803,10 @@ db_cache_folders(struct db *tdb)
if (tdb->folders) {
for (n = 0; n < tdb->nfolders; n++) {
- if (tdb->folders[n].bile)
+ if (tdb->folders[n].bile) {
bile_close(tdb->folders[n].bile);
+ xfree(&tdb->folders[n].bile);
+ }
}
xfree(&tdb->folders);
}
@@ -819,6 +837,8 @@ db_cache_folders(struct db *tdb)
size = bile_read_alloc(tdb->bile, DB_FOLDER_RTYPE, obj->id, &data);
if (size == 0 || data == NULL) {
+ if (data != NULL)
+ xfree(&data);
xfree(&obj);
break;
}
@@ -940,6 +960,7 @@ db_folder_delete(struct db *tdb, struct folder *folder
return;
}
bile_close(folder->bile);
+ xfree(&folder->bile);
}
const char *