Skip to content

Commit 34a89e5

Browse files
committed
Fix function ScrollViewCommand
The format string which builds the command only takes one or two string arguments, so the function allocated too much memory and passed too many arguments to snprintf. This also fixes a compiler warning (clang). Signed-off-by: Stefan Weil <sw@weilnetz.de>
1 parent 4d3b0bc commit 34a89e5

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

src/viewer/svutil.cpp

+11-6
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ struct addrinfo {
5050
#include <iostream>
5151
#include <memory>
5252
#include <string>
53+
#include <vector>
5354

5455
// Include automatically generated configuration file if running autoconf.
5556
#ifdef HAVE_CONFIG_H
@@ -301,19 +302,23 @@ static std::string ScrollViewCommand(std::string scrollview_path) {
301302
// this unnecessary.
302303
// Also the path has to be separated by ; on windows and : otherwise.
303304
#ifdef _WIN32
304-
const char* cmd_template = "-Djava.library.path=%s -jar %s/ScrollView.jar";
305+
const char cmd_template[] = "-Djava.library.path=%s -jar %s/ScrollView.jar";
305306

306307
#else
307-
const char* cmd_template =
308+
const char cmd_template[] =
308309
"-c \"trap 'kill %%1' 0 1 2 ; java "
309310
"-Xms1024m -Xmx2048m -jar %s/ScrollView.jar"
310311
" & wait\"";
311312
#endif
312-
size_t cmdlen = strlen(cmd_template) + 4 * strlen(scrollview_path.c_str()) + 1;
313-
std::unique_ptr<char[]> cmd(new char[cmdlen]);
313+
size_t cmdlen = sizeof(cmd_template) + 2 * scrollview_path.size() + 1;
314+
std::vector<char> cmd(cmdlen);
314315
const char* sv_path = scrollview_path.c_str();
315-
snprintf(cmd.get(), cmdlen, cmd_template, sv_path, sv_path, sv_path, sv_path);
316-
std::string command(cmd.get());
316+
#ifdef _WIN32
317+
snprintf(&cmd[0], cmdlen, cmd_template, sv_path, sv_path);
318+
#else
319+
snprintf(&cmd[0], cmdlen, cmd_template, sv_path);
320+
#endif
321+
std::string command(&cmd[0]);
317322
return command;
318323
}
319324

0 commit comments

Comments
 (0)