Skip to content

Commit

Permalink
Fix potential crash in tprintf
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Weil <[email protected]>
  • Loading branch information
stweil committed Nov 30, 2018
1 parent 6b6d9de commit e2419b1
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/ccutil/tprintf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ DLLSYM void tprintf(const char *format, ...)
{
tesseract::tprintfMutex.Lock();
va_list args; // variable args
const char* debug_file_name = debug_file.string();
static FILE *debugfp = nullptr; // debug file
// debug window
int32_t offset = 0; // into message
Expand All @@ -47,16 +48,16 @@ DLLSYM void tprintf(const char *format, ...)
// Format into msg
#ifdef _WIN32
offset += _vsnprintf(msg + offset, MAX_MSG_LEN - offset, format, args);
if (strcmp(debug_file.string(), "/dev/null") == 0)
if (debug_file_name && strcmp(debug_file_name, "/dev/null") == 0)
debug_file.set_value("nul");
#else
offset += vsnprintf(msg + offset, MAX_MSG_LEN - offset, format, args);
#endif
va_end(args);

if (debugfp == nullptr && strlen(debug_file.string()) > 0) {
if (debugfp == nullptr && debug_file_name && strlen(debug_file_name) > 0) {
debugfp = fopen(debug_file.string(), "wb");
} else if (debugfp != nullptr && strlen(debug_file.string()) == 0) {
} else if (debugfp != nullptr && debug_file_name && strlen(debug_file_name) == 0) {
fclose(debugfp);
debugfp = nullptr;
}
Expand Down

0 comments on commit e2419b1

Please sign in to comment.