backend_firefox: misc refactor

- Follow the "best practice" in the SQLite manual, where calls to
  sqlite3_column_bytes() should come after sqlite3_column_text().
  This change does not affect the values returned.
- Other misc updates.
This commit is contained in:
CismonX 2025-03-11 16:22:29 +08:00
parent 08b9ea81d6
commit 0a4ff8770e
No known key found for this signature in database
GPG key ID: 3094873E29A482FB
2 changed files with 23 additions and 16 deletions

View file

@ -214,7 +214,7 @@ struct mozbm_check_ctx {
}; };
struct mozbm_delete_ctx { struct mozbm_delete_ctx {
char buf[GUID_STR_LEN]; char guid_buf[GUID_STR_LEN];
char const *guid; char const *guid;
int64_t place_id; int64_t place_id;
@ -303,7 +303,7 @@ static int mozbm_pos_shift (struct backend_ctx *, int64_t, int64_t,
int64_t *, enum bookmarkfs_permd_op); int64_t *, enum bookmarkfs_permd_op);
static int mozbm_pos_update (struct backend_ctx *, int64_t, int64_t); static int mozbm_pos_update (struct backend_ctx *, int64_t, int64_t);
static int mozbm_update (struct backend_ctx *, struct mozbm *); static int mozbm_update (struct backend_ctx *, struct mozbm *);
static int mozbmdel_insert (struct backend_ctx *, char const *, int64_t); static int mozbmdel_insert (struct backend_ctx *, char const *);
static int mozkw_delete (struct backend_ctx *, char const *, size_t); static int mozkw_delete (struct backend_ctx *, char const *, size_t);
static int mozkw_insert (struct backend_ctx *, struct mozkw *); static int mozkw_insert (struct backend_ctx *, struct mozkw *);
static int mozkw_lookup (struct backend_ctx *, struct mozkw *); static int mozkw_lookup (struct backend_ctx *, struct mozkw *);
@ -643,8 +643,8 @@ mozbm_check_cb (
) { ) {
struct mozbm_check_ctx *ctx = user_data; struct mozbm_check_ctx *ctx = user_data;
size_t name_len = sqlite3_column_bytes(stmt, 0);
char const *name = (char const *)sqlite3_column_text(stmt, 0); char const *name = (char const *)sqlite3_column_text(stmt, 0);
size_t name_len = sqlite3_column_bytes(stmt, 0);
if (unlikely(name == NULL)) { if (unlikely(name == NULL)) {
name = ""; name = "";
} }
@ -716,7 +716,7 @@ mozbm_delete (
if (qctx.guid == NULL) { if (qctx.guid == NULL) {
return 0; return 0;
} }
return mozbmdel_insert(ctx, qctx.guid, usecs_now(NULL)); return mozbmdel_insert(ctx, qctx.guid);
} }
static int static int
@ -726,11 +726,12 @@ mozbm_delete_cb (
) { ) {
struct mozbm_delete_ctx *ctx = user_data; struct mozbm_delete_ctx *ctx = user_data;
size_t guid_len = sqlite3_column_bytes(stmt, 0); char const *guid = (char const *)sqlite3_column_text(stmt, 0);
size_t guid_len = sqlite3_column_bytes(stmt, 0);
if (guid_len != GUID_STR_LEN) { if (guid_len != GUID_STR_LEN) {
ctx->guid = NULL; ctx->guid = NULL;
} else { } else {
ctx->guid = memcpy(ctx->buf, sqlite3_column_text(stmt, 0), guid_len); ctx->guid = memcpy(ctx->guid_buf, guid, guid_len);
} }
ctx->place_id = sqlite3_column_int64(stmt, 1); ctx->place_id = sqlite3_column_int64(stmt, 1);
@ -1057,14 +1058,18 @@ mozbm_update (
static int static int
mozbmdel_insert ( mozbmdel_insert (
struct backend_ctx *ctx, struct backend_ctx *ctx,
char const *guid, char const *guid
int64_t date_removed
) { ) {
sqlite3_stmt **stmt_ptr = &ctx->stmts[STMT_MOZBMDEL_INSERT]; sqlite3_stmt **stmt_ptr = &ctx->stmts[STMT_MOZBMDEL_INSERT];
char const *sql = char const *sql =
"INSERT OR IGNORE INTO `moz_bookmarks_deleted` (`guid`, `dateRemoved`)" "INSERT OR IGNORE INTO `moz_bookmarks_deleted` (`guid`, `dateRemoved`)"
"VALUES (?, ?)"; "VALUES (?, ?)";
int64_t date_removed = usecs_now(NULL);
if (unlikely(date_removed < 0)) {
return -EIO;
}
int status; int status;
DO_QUERY(ctx, stmt_ptr, sql, NULL, NULL, status, , , DO_QUERY(ctx, stmt_ptr, sql, NULL, NULL, status, , ,
DB_QUERY_BIND_TEXT(guid, GUID_STR_LEN), DB_QUERY_BIND_TEXT(guid, GUID_STR_LEN),
@ -2374,8 +2379,8 @@ bookmark_check_cb (
return 0; return 0;
} }
size_t name_len = sqlite3_column_bytes(stmt, 2);
char const *name = (char const *)sqlite3_column_text(stmt, 2); char const *name = (char const *)sqlite3_column_text(stmt, 2);
size_t name_len = sqlite3_column_bytes(stmt, 2);
if (unlikely(name == NULL)) { if (unlikely(name == NULL)) {
name = ""; name = "";
} }
@ -2433,10 +2438,13 @@ bookmark_get_cb (
) { ) {
struct bookmark_get_ctx *ctx = user_data; struct bookmark_get_ctx *ctx = user_data;
size_t len = sqlite3_column_bytes(stmt, 0);
char const *val = (char const *)sqlite3_column_text(stmt, 0); char const *val = (char const *)sqlite3_column_text(stmt, 0);
size_t len = sqlite3_column_bytes(stmt, 0);
if (unlikely(val == NULL)) {
val = "";
}
ctx->status = ctx->callback(ctx->user_data, val == NULL ? "" : val, len); ctx->status = ctx->callback(ctx->user_data, val, len);
return 1; return 1;
} }
@ -2465,8 +2473,8 @@ bookmark_list_cb (
} }
entry.off = position + 1; entry.off = position + 1;
size_t name_len = sqlite3_column_bytes(stmt, 2);
char const *name = (char const *)sqlite3_column_text(stmt, 2); char const *name = (char const *)sqlite3_column_text(stmt, 2);
size_t name_len = sqlite3_column_bytes(stmt, 2);
if (unlikely(name == NULL)) { if (unlikely(name == NULL)) {
name = ""; name = "";
} }

View file

@ -69,8 +69,8 @@ db_pragma_cb (
) { ) {
struct db_pragma_ctx *ctx = user_data; struct db_pragma_ctx *ctx = user_data;
size_t val_len = sqlite3_column_bytes(stmt, 0); char const *val = (char const *)sqlite3_column_text(stmt, 0);
unsigned char const *val = sqlite3_column_text(stmt, 0); size_t val_len = sqlite3_column_bytes(stmt, 0);
xassert(val != NULL); xassert(val != NULL);
ctx->status = 0; ctx->status = 0;
@ -265,8 +265,7 @@ db_pragma (
return -1; return -1;
} }
again = false; again = false;
// strlen(" = ") == 3 sql_len = pragma_ctx.val - sql - strlen(" = ");
sql_len = pragma_ctx.val - sql - 3;
goto query; goto query;
} }
} }