Commit e66738c4 for tesseract
commit e66738c42594fc0b5354c4dac0b830d392db3f39
Author: Stefan Weil <sw@weilnetz.de>
Date: Fri Nov 27 14:07:14 2026 +0100
Replace memcpy in two places by loops
The static code analysis which is run for pull requests (Codacy,
Semgrep rule memcpy-CopyMemory) reports each new call of memcpy as a
critical issue. The previous commit added two of them, in
CHAR_FRAGMENT::set_unichar() and in a helper function of
intproto_test.cc.
Both copied a few bytes into a buffer which was large enough. Copy
them in a loop.
Assisted-by: Claude Fable 5.2 (Anthropic)
Signed-off-by: Stefan Weil <sw@weilnetz.de>
diff --git a/src/ccutil/unicharset.h b/src/ccutil/unicharset.h
index 55f58452..94cce98d 100644
--- a/src/ccutil/unicharset.h
+++ b/src/ccutil/unicharset.h
@@ -64,9 +64,14 @@ public:
set_natural(natural);
}
inline void set_unichar(const char *uch) {
- const size_t len = strnlen(uch, UNICHAR_LEN);
- memcpy(this->unichar, uch, len);
- memset(this->unichar + len, 0, sizeof(this->unichar) - len);
+ // Copy at most UNICHAR_LEN characters and clear the rest.
+ size_t i = 0;
+ for (; i < UNICHAR_LEN && uch[i] != '\0'; ++i) {
+ this->unichar[i] = uch[i];
+ }
+ for (; i < sizeof(this->unichar); ++i) {
+ this->unichar[i] = '\0';
+ }
}
inline void set_pos(int p) {
this->pos = p;
diff --git a/unittest/intproto_test.cc b/unittest/intproto_test.cc
index ff934c7f..fcc1a964 100644
--- a/unittest/intproto_test.cc
+++ b/unittest/intproto_test.cc
@@ -28,7 +28,6 @@
#include <cstdint>
#include <cstdio>
-#include <cstring> // for memcpy
#include <string>
#include <vector>
@@ -51,9 +50,10 @@ std::vector<char> MakeInttemp(int32_t version_id, uint32_t num_class_pruners,
uint8_t num_proto_sets = 0, uint16_t num_protos = 0) {
std::vector<char> data;
auto append = [&data](const void *p, size_t n) {
- const size_t old_size = data.size();
- data.resize(old_size + n);
- memcpy(&data[old_size], p, n);
+ const char *b = static_cast<const char *>(p);
+ for (size_t i = 0; i < n; ++i) {
+ data.push_back(b[i]);
+ }
};
append(&unicharset_size, sizeof(unicharset_size));
append(&version_id, sizeof(version_id));
@@ -90,9 +90,10 @@ std::vector<char> MakeLegacyInttemp(int32_t version_id, uint32_t num_classes,
const std::vector<int32_t> &class_ids) {
std::vector<char> data;
auto append = [&data](const void *p, size_t n) {
- const size_t old_size = data.size();
- data.resize(old_size + n);
- memcpy(&data[old_size], p, n);
+ const char *b = static_cast<const char *>(p);
+ for (size_t i = 0; i < n; ++i) {
+ data.push_back(b[i]);
+ }
};
uint32_t num_class_pruners = 1;
append(&unicharset_size, sizeof(unicharset_size));