Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace deprecated sprintf #4068

Merged
merged 1 commit into from
May 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions src/ccmain/paramsd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
# include "svmnode.h" // for SVMenuNode
# include "tesseractclass.h" // for Tesseract

# include <cstdio> // for fclose, fopen, fprintf, sprintf, FILE
# include <cstdio> // for fclose, fopen, fprintf, FILE
# include <cstdlib> // for atoi
# include <cstring> // for strcmp, strcspn, strlen, strncpy
# include <locale> // for std::locale::classic
Expand Down Expand Up @@ -319,16 +319,12 @@ ParamsEditor::ParamsEditor(tesseract::Tesseract *tess, ScrollView *sv) {
// Write all (changed_) parameters to a config file.
void ParamsEditor::WriteParams(char *filename, bool changes_only) {
FILE *fp; // input file
char msg_str[255];
// if file exists
if ((fp = fopen(filename, "rb")) != nullptr) {
fclose(fp);
sprintf(msg_str,
"Overwrite file "
"%s"
"? (Y/N)",
filename);
int a = sv_window_->ShowYesNoDialog(msg_str);
std::stringstream msg;
msg << "Overwrite file " << filename << "? (Y/N)";
int a = sv_window_->ShowYesNoDialog(msg.str().c_str());
if (a == 'n') {
return;
} // don't write
Expand Down
25 changes: 13 additions & 12 deletions src/ccmain/pgedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@

#include <cctype>
#include <cmath>
#include <iomanip> // for std::setprecision
#include <locale> // for std::locale::classic
#include <sstream> // for std::stringstream

#ifndef GRAPHICS_DISABLED
namespace tesseract {
Expand Down Expand Up @@ -140,32 +143,30 @@ static void show_point(PAGE_RES *page_res, float x, float y) {
FCOORD pt(x, y);
PAGE_RES_IT pr_it(page_res);

const int kBufsize = 512;
char msg[kBufsize];
char *msg_ptr = msg;

msg_ptr += sprintf(msg_ptr, "Pt:(%0.3f, %0.3f) ", x, y);
std::stringstream msg;
msg.imbue(std::locale::classic());
msg << std::fixed << std::setprecision(3) << "Pt:(" << x << ", " << y << ") ";

for (WERD_RES *word = pr_it.word(); word != nullptr; word = pr_it.forward()) {
if (pr_it.row() != pr_it.prev_row() && pr_it.row()->row->bounding_box().contains(pt)) {
msg_ptr += sprintf(msg_ptr, "BL(x)=%0.3f ", pr_it.row()->row->base_line(x));
msg << "BL(x)=" << pr_it.row()->row->base_line(x) << ' ';
}
if (word->word->bounding_box().contains(pt)) {
TBOX box = word->word->bounding_box();
msg_ptr += sprintf(msg_ptr, "Wd(%d, %d)/(%d, %d) ", box.left(), box.bottom(), box.right(),
box.top());
msg << "Wd(" << box.left() << ", " << box.bottom() << ")/("
<< box.right() << ", " << box.top() << ") ";
C_BLOB_IT cblob_it(word->word->cblob_list());
for (cblob_it.mark_cycle_pt(); !cblob_it.cycled_list(); cblob_it.forward()) {
C_BLOB *cblob = cblob_it.data();
box = cblob->bounding_box();
if (box.contains(pt)) {
msg_ptr += sprintf(msg_ptr, "CBlb(%d, %d)/(%d, %d) ", box.left(), box.bottom(),
box.right(), box.top());
msg << "CBlb(" << box.left() << ", " << box.bottom() << ")/("
<< box.right() << ", " << box.top() << ") ";
}
}
}
}
image_win->AddMessage(msg);
image_win->AddMessage(msg.str().c_str());
}

/**
Expand Down Expand Up @@ -622,7 +623,7 @@ void Tesseract::process_image_event( // action in image win
break;

default:
sprintf(msg, "Mode %d not yet implemented", mode);
snprintf(msg, sizeof(msg), "Mode %d not yet implemented", mode);
image_win->AddMessage(msg);
break;
}
Expand Down
33 changes: 12 additions & 21 deletions src/ccutil/errcode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream> // for std::cerr
#include <sstream> // for std::stringstream

namespace tesseract {

Expand All @@ -41,37 +43,26 @@ void ERRCODE::error( // handle error
const char *format, ... // special message
) const {
va_list args; // variable args
char msg[MAX_MSG];
char *msgptr = msg;
std::stringstream msg;

if (caller != nullptr) {
// name of caller
msgptr += sprintf(msgptr, "%s:", caller);
msg << caller << ':';
}
// actual message
msgptr += sprintf(msgptr, "Error:%s", message);
msg << "Error:" << message;
if (format != nullptr) {
msgptr += sprintf(msgptr, ":");
char str[MAX_MSG];
va_start(args, format); // variable list
#ifdef _WIN32
// print remainder
msgptr += _vsnprintf(msgptr, MAX_MSG - 2 - (msgptr - msg), format, args);
msg[MAX_MSG - 2] = '\0'; // ensure termination
strcat(msg, "\n");
#else
// print remainder
msgptr += vsprintf(msgptr, format, args);
// no specific
msgptr += sprintf(msgptr, "\n");
#endif
// print remainder
std::vsnprintf(str, sizeof(str), format, args);
// ensure termination
str[sizeof(str) - 1] = '\0';
va_end(args);
} else {
// no specific
msgptr += sprintf(msgptr, "\n");
msg << ':' << str;
}

// %s is needed here so msg is printed correctly!
fprintf(stderr, "%s", msg);
std::cerr << msg.str() << '\n';

switch (action) {
case DBG:
Expand Down
4 changes: 2 additions & 2 deletions src/ccutil/unicharset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -314,10 +314,10 @@ std::string UNICHARSET::debug_utf8_str(const char *str) {
step = UNICHAR::utf8_step(str + i);
if (step == 0) {
step = 1;
sprintf(hex, "%x", str[i]);
snprintf(hex, sizeof(hex), "%x", str[i]);
} else {
UNICHAR ch(str + i, step);
sprintf(hex, "%x", ch.first_uni());
snprintf(hex, sizeof(hex), "%x", ch.first_uni());
}
result += hex;
result += " ";
Expand Down
11 changes: 6 additions & 5 deletions src/opencl/openclwrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -812,15 +812,16 @@ int OpenclDevice::BinaryGenerated(const char *clFileName, FILE **fhandle) {
cl_int clStatus;
int status = 0;
FILE *fd = nullptr;
char fileName[256] = {0}, cl_name[128] = {0};
char fileName[256];
char cl_name[128];
char deviceName[1024];
clStatus = clGetDeviceInfo(gpuEnv.mpArryDevsID[i], CL_DEVICE_NAME, sizeof(deviceName), deviceName,
nullptr);
CHECK_OPENCL(clStatus, "clGetDeviceInfo");
const char *str = strstr(clFileName, ".cl");
memcpy(cl_name, clFileName, str - clFileName);
cl_name[str - clFileName] = '\0';
sprintf(fileName, "%s-%s.bin", cl_name, deviceName);
snprintf(fileName, sizeof(fileName), "%s-%s.bin", cl_name, deviceName);
legalizeFileName(fileName);
fd = fopen(fileName, "rb");
status = (fd != nullptr) ? 1 : 0;
Expand Down Expand Up @@ -894,9 +895,9 @@ int OpenclDevice::GeneratBinFromKernelSource(cl_program program, const char *clF

/* dump out each binary into its own separate file. */
for (i = 0; i < numDevices; i++) {
char fileName[256] = {0}, cl_name[128] = {0};

if (binarySizes[i] != 0) {
char fileName[256];
char cl_name[128];
char deviceName[1024];
clStatus =
clGetDeviceInfo(mpArryDevsID[i], CL_DEVICE_NAME, sizeof(deviceName), deviceName, nullptr);
Expand All @@ -905,7 +906,7 @@ int OpenclDevice::GeneratBinFromKernelSource(cl_program program, const char *clF
const char *str = strstr(clFileName, ".cl");
memcpy(cl_name, clFileName, str - clFileName);
cl_name[str - clFileName] = '\0';
sprintf(fileName, "%s-%s.bin", cl_name, deviceName);
snprintf(fileName, sizeof(fileName), "%s-%s.bin", cl_name, deviceName);
legalizeFileName(fileName);
if (!WriteBinaryToFile(fileName, binaries[i], binarySizes[i])) {
tprintf("[OD] write binary[%s] failed\n", fileName);
Expand Down
4 changes: 2 additions & 2 deletions src/wordrec/findseam.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ void Wordrec::add_seam_to_queue(float new_priority, SEAM *new_seam, SeamQueue *s
void Wordrec::choose_best_seam(SeamQueue *seam_queue, const SPLIT *split, PRIORITY priority,
SEAM **seam_result, TBLOB *blob, SeamPile *seam_pile) {
SEAM *seam;
char str[80];
float my_priority;
/* Add seam of split */
my_priority = priority;
Expand Down Expand Up @@ -133,7 +132,8 @@ void Wordrec::choose_best_seam(SeamQueue *seam_queue, const SPLIT *split, PRIORI
seam->FullPriority(bbox.left(), bbox.right(), chop_overlap_knob, chop_centered_maxwidth,
chop_center_knob, chop_width_change_knob);
if (chop_debug) {
sprintf(str, "Full my_priority %0.0f, ", my_priority);
char str[80];
snprintf(str, sizeof(str), "Full my_priority %0.0f, ", my_priority);
seam->Print(str);
}

Expand Down