--- lbbs/src/article_view_log.c 2025/06/17 13:17:04 1.6 +++ lbbs/src/article_view_log.c 2025/12/19 06:16:26 1.19 @@ -1,23 +1,19 @@ -/*************************************************************************** - article_view_log.c - description - ------------------- - Copyright : (C) 2004-2025 by Leaflet - Email : leaflet@leafok.com - ***************************************************************************/ - -/*************************************************************************** - * * - * This program is free software; you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation; either version 3 of the License, or * - * (at your option) any later version. * - * * - ***************************************************************************/ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +/* + * article_view_log + * - data persistence and query of article view log + * + * Copyright (C) 2004-2025 Leaflet + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif #include "article_view_log.h" -#include "log.h" #include "common.h" #include "database.h" +#include "log.h" #include #include @@ -32,7 +28,7 @@ int article_view_log_load(int uid, ARTIC if (p_view_log == NULL) { - log_error("article_view_log_load() error: NULL pointer\n"); + log_error("NULL pointer error"); return -1; } @@ -53,7 +49,7 @@ int article_view_log_load(int uid, ARTIC if ((db = db_open()) == NULL) { - log_error("article_view_log_load() error: Unable to open DB\n"); + log_error("article_view_log_load() error: Unable to open DB"); return -2; } @@ -63,12 +59,12 @@ int article_view_log_load(int uid, ARTIC uid); if (mysql_query(db, sql) != 0) { - log_error("Query view_article_log error: %s\n", mysql_error(db)); + log_error("Query view_article_log error: %s", mysql_error(db)); return -3; } if ((rs = mysql_store_result(db)) == NULL) { - log_error("Get view_article_log data failed\n"); + log_error("Get view_article_log data failed"); return -3; } @@ -76,7 +72,7 @@ int article_view_log_load(int uid, ARTIC p_view_log->aid_base = malloc(sizeof(int32_t) * mysql_num_rows(rs)); if (p_view_log->aid_base == NULL) { - log_error("malloc(INT32 * %d) error: OOM\n", mysql_num_rows(rs)); + log_error("malloc(INT32 * %d) error: OOM", mysql_num_rows(rs)); mysql_free_result(rs); mysql_close(db); return -4; @@ -90,7 +86,7 @@ int article_view_log_load(int uid, ARTIC mysql_close(db); - log_common("Loaded %d view_article_log records for uid=%d\n", p_view_log->aid_base_cnt, uid); + log_common("Loaded %d view_article_log records for uid=%d", p_view_log->aid_base_cnt, uid); if (!keep_inc) { @@ -104,7 +100,7 @@ int article_view_log_unload(ARTICLE_VIEW { if (p_view_log == NULL) { - log_error("article_view_log_unload() error: NULL pointer\n"); + log_error("NULL pointer error"); return -1; } @@ -128,18 +124,18 @@ int article_view_log_save_inc(const ARTI if (p_view_log == NULL) { - log_error("article_view_log_save_inc() error: NULL pointer\n"); + log_error("NULL pointer error"); return -1; } - if (p_view_log->uid <= 0) + if (p_view_log->uid <= 0 || p_view_log->aid_inc_cnt == 0) { return 0; } if ((db = db_open()) == NULL) { - log_error("article_view_log_load() error: Unable to open DB\n"); + log_error("article_view_log_load() error: Unable to open DB"); return -2; } @@ -157,7 +153,7 @@ int article_view_log_save_inc(const ARTI { if (mysql_query(db, sql) != 0) { - log_error("Add view_article_log error: %s\n", mysql_error(db)); + log_error("Add view_article_log error: %s", mysql_error(db)); mysql_close(db); return -3; } @@ -173,7 +169,7 @@ int article_view_log_save_inc(const ARTI } } - log_common("Saved %d view_article_log records for uid=%d\n", affected_record, p_view_log->uid); + log_common("Saved %d view_article_log records for uid=%d", affected_record, p_view_log->uid); mysql_close(db); @@ -185,10 +181,11 @@ int article_view_log_merge_inc(ARTICLE_V int32_t *aid_new; int aid_new_cnt; int i, j, k; + int len; if (p_view_log == NULL) { - log_error("article_view_log_merge_inc() error: NULL pointer\n"); + log_error("NULL pointer error"); return -1; } @@ -202,7 +199,7 @@ int article_view_log_merge_inc(ARTICLE_V aid_new = malloc(sizeof(int32_t) * (size_t)aid_new_cnt); if (aid_new == NULL) { - log_error("malloc(INT32 * %d) error: OOM\n", aid_new_cnt); + log_error("malloc(INT32 * %d) error: OOM", aid_new_cnt); return -2; } @@ -212,7 +209,7 @@ int article_view_log_merge_inc(ARTICLE_V { if (p_view_log->aid_base[i] == p_view_log->aid_inc[j]) { - log_error("Duplicate aid = %d found in both Base (offset = %d) and Inc (offset = %d)\n", + log_error("Duplicate aid = %d found in both Base (offset = %d) and Inc (offset = %d)", p_view_log->aid_base[i], i, j); j++; // Skip duplicate one in Inc } @@ -225,10 +222,20 @@ int article_view_log_merge_inc(ARTICLE_V } } - memcpy(aid_new + k, p_view_log->aid_base + i, sizeof(int32_t) * (size_t)(p_view_log->aid_base_cnt - i)); - k += (p_view_log->aid_base_cnt - i); - memcpy(aid_new + k, p_view_log->aid_inc + j, sizeof(int32_t) * (size_t)(p_view_log->aid_inc_cnt - j)); - k += (p_view_log->aid_inc_cnt - j); + len = p_view_log->aid_base_cnt - i; + if (len > 0) + { + memcpy(aid_new + k, p_view_log->aid_base + i, + sizeof(int32_t) * (size_t)len); + k += len; + } + len = p_view_log->aid_inc_cnt - j; + if (len > 0) + { + memcpy(aid_new + k, p_view_log->aid_inc + j, + sizeof(int32_t) * (size_t)len); + k += len; + } free(p_view_log->aid_base); p_view_log->aid_base = aid_new; @@ -248,7 +255,7 @@ int article_view_log_is_viewed(int32_t a if (p_view_log == NULL) { - log_error("article_view_log_is_viewed() error: NULL pointer\n"); + log_error("NULL pointer error"); return -1; } @@ -267,7 +274,7 @@ int article_view_log_is_viewed(int32_t a mid = (left + right) / 2; if (aid < (i == 0 ? p_view_log->aid_base[mid] : p_view_log->aid_inc[mid])) { - right = mid; + right = mid - 1; } else if (aid > (i == 0 ? p_view_log->aid_base[mid] : p_view_log->aid_inc[mid])) { @@ -297,7 +304,7 @@ int article_view_log_set_viewed(int32_t if (p_view_log == NULL) { - log_error("article_view_log_set_viewed() error: NULL pointer\n"); + log_error("NULL pointer error"); return -1; } @@ -316,7 +323,7 @@ int article_view_log_set_viewed(int32_t mid = (left + right) / 2; if (aid < (i == 0 ? p_view_log->aid_base[mid] : p_view_log->aid_inc[mid])) { - right = mid; + right = mid - 1; } else if (aid > (i == 0 ? p_view_log->aid_base[mid] : p_view_log->aid_inc[mid])) { @@ -335,12 +342,12 @@ int article_view_log_set_viewed(int32_t } // Merge if Inc is full - if (p_view_log->aid_inc_cnt >= MAX_AID_INC_CNT) + if (p_view_log->aid_inc_cnt >= MAX_VIEWED_AID_INC_CNT) { // Save incremental article view log if (article_view_log_save_inc(p_view_log) < 0) { - log_error("article_view_log_save_inc() error\n"); + log_error("article_view_log_save_inc() error"); return -2; } @@ -360,9 +367,11 @@ int article_view_log_set_viewed(int32_t right = left + 1; } - for (i = p_view_log->aid_inc_cnt - 1; i >= right; i--) + if (p_view_log->aid_inc_cnt > right) { - p_view_log->aid_inc[i + 1] = p_view_log->aid_inc[i]; + memmove(p_view_log->aid_inc + right + 1, + p_view_log->aid_inc + right, + sizeof(int32_t) * (size_t)(p_view_log->aid_inc_cnt - right)); } p_view_log->aid_inc[right] = aid;