Skip to content

Commit

Permalink
Replace unsecure _splitpath by secure _splitpath_s
Browse files Browse the repository at this point in the history
Use the predefined macros for the lengths of drive, dir and path.
This avoids potential buffer overruns.
Show also an error message in case of a too long path.

Signed-off-by: Noah Metzger <noah.metzger@bib.uni-mannheim.de>
  • Loading branch information
noahmetzger committed Apr 12, 2018
1 parent b7b6b28 commit d88a6b5
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions ccutil/mainblk.cpp
Expand Up @@ -64,12 +64,17 @@ void CCUtil::main_setup(const char *argv0, const char *basename) {
#if defined(_WIN32)
} else if (datadir == NULL || access(datadir.string(), 0) != 0) {
/* Look for tessdata in directory of executable. */
static char drive[4];
static char dir[128];
static char exe[128];
DWORD length = GetModuleFileName(NULL, exe, sizeof(exe));
if (length > 0 && length < sizeof(exe)) {
_splitpath(exe, drive, dir, NULL, NULL);
char drive[_MAX_DRIVE];
char dir[_MAX_DIR];
char path[_MAX_PATH];
DWORD length = GetModuleFileName(NULL, path, sizeof(path));
if (length > 0 && length < sizeof(path)) {
errno_t result = _splitpath_s(path, drive, sizeof(drive),
dir, sizeof(dir), NULL, 0, NULL, 0);
if (result == ERANGE) {
tprintf("Error: Path too long: %s\n", path);
}

datadir = drive;
datadir += dir;
}
Expand Down

0 comments on commit d88a6b5

Please sign in to comment.