From 053ae79e97597a51cab07cc97a2569a8420b2e41 Mon Sep 17 00:00:00 2001 From: Bo-Cheng Jhan Date: Tue, 11 Sep 2018 22:57:50 +0800 Subject: [PATCH 1/2] hash.c: Fix strncpy warning --- src/hash.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hash.c b/src/hash.c index 33e825fbf..2ce04f0ef 100644 --- a/src/hash.c +++ b/src/hash.c @@ -522,7 +522,7 @@ int InitUserphrase(struct ChewingData *pgdata, const char *path) int item_index, hashvalue, iret, fsize, hdrlen, oldest = INT_MAX; char *dump, *seekdump; - strncpy(pgdata->static_data.hashfilename, path, sizeof(pgdata->static_data.hashfilename)); + strncpy(pgdata->static_data.hashfilename, path, sizeof(pgdata->static_data.hashfilename) - 1); memset(pgdata->static_data.hashtable, 0, sizeof(pgdata->static_data.hashtable)); open_hash_file: From 6dae670620da502efdbfb96645ccb769b4060f92 Mon Sep 17 00:00:00 2001 From: Bo-Cheng Jhan Date: Wed, 12 Sep 2018 21:54:48 +0800 Subject: [PATCH 2/2] Use ARRAY_SIZE instead of sizeof in strncpy expressions --- src/chewingio.c | 2 +- src/chewingutil.c | 4 ++-- src/hash.c | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/chewingio.c b/src/chewingio.c index 2473c3bf1..704c649e9 100644 --- a/src/chewingio.c +++ b/src/chewingio.c @@ -193,7 +193,7 @@ CHEWING_API ChewingContext *chewing_new2(const char *syspath, chewing_Reset(ctx); if (syspath) { - strncpy(search_path, syspath, sizeof(search_path) - 1); + strncpy(search_path, syspath, ARRAY_SIZE(search_path) - 1); } else { ret = get_search_path(search_path, sizeof(search_path)); if (ret) { diff --git a/src/chewingutil.c b/src/chewingutil.c index 73cb78bef..01f6b6cfa 100644 --- a/src/chewingutil.c +++ b/src/chewingutil.c @@ -152,7 +152,7 @@ static int _Inner_InternalSpecialSymbol(int key, ChewingData *pgdata, char symke &pgdata->preeditBuf[pgdata->chiSymbolCursor], sizeof(pgdata->preeditBuf[0]) * (pgdata->chiSymbolBufLen - pgdata->chiSymbolCursor)); - strncpy(buf->char_, chibuf, sizeof(buf->char_) - 1); + strncpy(buf->char_, chibuf, ARRAY_SIZE(buf->char_) - 1); buf->category = CHEWING_SYMBOL; /* Save Symbol Key */ @@ -356,7 +356,7 @@ int SymbolChoice(ChewingData *pgdata, int sel_i) symbol_type = SYMBOL_CHOICE_UPDATE; } } - strncpy(buf->char_, pgdata->choiceInfo.totalChoiceStr[sel_i], sizeof(buf->char_) - 1); + strncpy(buf->char_, pgdata->choiceInfo.totalChoiceStr[sel_i], ARRAY_SIZE(buf->char_) - 1); buf->category = CHEWING_SYMBOL; /* This is very strange */ diff --git a/src/hash.c b/src/hash.c index 2ce04f0ef..7eb8f09f9 100644 --- a/src/hash.c +++ b/src/hash.c @@ -522,7 +522,7 @@ int InitUserphrase(struct ChewingData *pgdata, const char *path) int item_index, hashvalue, iret, fsize, hdrlen, oldest = INT_MAX; char *dump, *seekdump; - strncpy(pgdata->static_data.hashfilename, path, sizeof(pgdata->static_data.hashfilename) - 1); + strncpy(pgdata->static_data.hashfilename, path, ARRAY_SIZE(pgdata->static_data.hashfilename) - 1); memset(pgdata->static_data.hashtable, 0, sizeof(pgdata->static_data.hashtable)); open_hash_file: