Skip to content

Commit e6c2e77

Browse files
committed
Fixed GCC build warnings
1 parent 88ffb28 commit e6c2e77

File tree

3 files changed

+78
-61
lines changed

3 files changed

+78
-61
lines changed

shell/shell_resultset_dumper.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program is free software; you can redistribute it and/or
55
* modify it under the terms of the GNU General Public License as
@@ -22,7 +22,7 @@
2222

2323
#include <stdlib.h>
2424
#include <iostream>
25-
#include "cmdline_options.h"
25+
#include "src/cmdline_options.h"
2626
#include "modules/base_resultset.h"
2727
#include "shellcore/lang_base.h"
2828

src/shell_cmdline_options.cc

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,17 @@
2020
#include <stdlib.h>
2121
#include <iostream>
2222
#include "shellcore/ishell_core.h"
23-
#include "shell_cmdline_options.h"
23+
#include "src/shell_cmdline_options.h"
2424
#include "utils/utils_general.h"
2525
#include "utils/utils_connection.h"
2626
#include "utils/uri_parser.h"
2727
#include <boost/format.hpp>
2828
#include <boost/lexical_cast.hpp>
2929
#include <boost/algorithm/string.hpp>
3030

31-
Shell_command_line_options::Shell_command_line_options(int argc, char **argv)
32-
: Command_line_options(argc, argv) {
33-
31+
Shell_command_line_options::Shell_command_line_options(int argc,
32+
char **argv)
33+
: Command_line_options(argc, argv) {
3434
int arg_format = 0;
3535
for (int i = 1; i < argc && exit_code == 0; i++) {
3636
char *value;
@@ -61,7 +61,7 @@ Shell_command_line_options::Shell_command_line_options(int argc, char **argv)
6161
if (arg_format == 3)
6262
nopwd_uri = "--uri=" + nopwd_uri;
6363

64-
strcpy(argv[i], nopwd_uri.substr(0, nopwd_uri.length()).c_str());
64+
strncpy(argv[i], nopwd_uri.c_str(), strlen(argv[i]) + 1);
6565
}
6666
} else {
6767
std::cerr << "Invalid value specified in --uri parameter.\n";
@@ -118,7 +118,7 @@ Shell_command_line_options::Shell_command_line_options(int argc, char **argv)
118118
std::string pwd = arg_format == 2 ? "-p" : "--dbpassword=";
119119
pwd.append(stars);
120120

121-
strcpy(argv[i], pwd.c_str());
121+
strncpy(argv[i], pwd.c_str(), strlen(argv[i]) + 1);
122122
}
123123

124124
// --password value (value is ignored)
@@ -155,7 +155,7 @@ Shell_command_line_options::Shell_command_line_options(int argc, char **argv)
155155
std::string pwd = arg_format == 2 ? "-p" : "--password=";
156156
pwd.append(stars);
157157

158-
strcpy(argv[i], pwd.c_str());
158+
strncpy(argv[i], pwd.c_str(), strlen(argv[i]) + 1);
159159
}
160160

161161
// --password value (value is ignored)
@@ -306,8 +306,10 @@ Shell_command_line_options::Shell_command_line_options(int argc, char **argv)
306306
(*data)["dbPassword"] = shcore::Value(pwd);
307307

308308
// Hide password being used.
309-
auto nopwd_uri = shcore::build_connection_string(data, true);
310-
strcpy(argv[i], nopwd_uri.substr(0, _options.uri.length()).c_str());
309+
std::string nopwd_uri = shcore::build_connection_string(data, true);
310+
std::string nopwd_stripped =
311+
nopwd_uri.substr(0, _options.uri.length());
312+
strncpy(argv[i], nopwd_stripped.c_str(), strlen(argv[i]) + 1);
311313
}
312314
} else {
313315
std::cerr << "Invalid uri parameter.\n";

unittest/shell_cmdline_options_t.cc

Lines changed: 65 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ class Shell_cmdline_options : public ::testing::Test {
436436

437437
TEST_F(Shell_cmdline_options, default_values) {
438438
int argc = 0;
439-
char **argv = NULL;
439+
char **argv = nullptr;
440440

441441
Shell_command_line_options cmd_options(argc, argv);
442442
mysqlsh::Shell_options options = cmd_options.get_options();
@@ -719,9 +719,9 @@ TEST_F(Shell_cmdline_options, conflicts_session_type) {
719719
"Classic session configured with --classic.\n";
720720

721721
char *argv0[] = {
722-
"ut",
723-
"--classic",
724-
"--uri=mysqlx://root@localhost",
722+
const_cast<char *>("ut"),
723+
const_cast<char *>("--classic"),
724+
const_cast<char *>("--uri=mysqlx://root@localhost"),
725725
NULL
726726
};
727727

@@ -733,9 +733,9 @@ TEST_F(Shell_cmdline_options, conflicts_session_type) {
733733
"Classic session configured with --sqlc.\n";
734734

735735
char *argv0[] = {
736-
"ut",
737-
"--sqlc",
738-
"--uri=mysqlx://root@localhost",
736+
const_cast<char *>("ut"),
737+
const_cast<char *>("--sqlc"),
738+
const_cast<char *>("--uri=mysqlx://root@localhost"),
739739
NULL
740740
};
741741

@@ -747,9 +747,9 @@ TEST_F(Shell_cmdline_options, conflicts_session_type) {
747747
"Node session configured with --node.\n";
748748

749749
char *argv1[] = {
750-
"ut",
751-
"--node",
752-
"--uri=mysql://root@localhost",
750+
const_cast<char *>("ut"),
751+
const_cast<char *>("--node"),
752+
const_cast<char *>("--uri=mysql://root@localhost"),
753753
NULL
754754
};
755755

@@ -761,9 +761,9 @@ TEST_F(Shell_cmdline_options, conflicts_session_type) {
761761
"Node session configured with --sqln.\n";
762762

763763
char *argv1[] = {
764-
"ut",
765-
"--sqln",
766-
"--uri=mysql://root@localhost",
764+
const_cast<char *>("ut"),
765+
const_cast<char *>("--sqln"),
766+
const_cast<char *>("--uri=mysql://root@localhost"),
767767
NULL
768768
};
769769

@@ -777,9 +777,9 @@ TEST_F(Shell_cmdline_options, conflicts_user) {
777777
"user in the URI.\n";
778778

779779
char *argv0[] = {
780-
"ut",
781-
"--user=guest",
782-
"--uri=mysqlx://root@localhost",
780+
const_cast<char *>("ut"),
781+
const_cast<char *>("--user=guest"),
782+
const_cast<char *>("--uri=mysqlx://root@localhost"),
783783
NULL
784784
};
785785

@@ -793,7 +793,7 @@ TEST_F(Shell_cmdline_options, conflicts_password) {
793793
char pwd[] = {"--password=example"};
794794
char uri[] = {"--uri=mysqlx://root:password@localhost"};
795795
char *argv0[] = {
796-
"ut",
796+
const_cast<char *>("ut"),
797797
pwd,
798798
uri,
799799
NULL
@@ -808,8 +808,8 @@ TEST_F(Shell_cmdline_options, conflicts_host) {
808808

809809
char uri[] = "--uri=mysqlx://root:password@localhost";
810810
char *argv0[] = {
811-
"ut",
812-
"--host=127.0.0.1",
811+
const_cast<char *>("ut"),
812+
const_cast<char *>("--host=127.0.0.1"),
813813
uri,
814814
NULL
815815
};
@@ -822,20 +822,20 @@ TEST_F(Shell_cmdline_options, conflicts_host_socket) {
822822
"not 'localhost'.\n";
823823

824824
char *argv0[] = {
825-
"ut",
826-
827-
"--socket=/some/socket/path",
825+
const_cast<char *>("ut"),
826+
const_cast<char *>("[email protected]"),
827+
const_cast<char *>("--socket=/some/socket/path"),
828828
NULL
829829
};
830830
test_conflicting_options("--uri --socket", 3, argv0, error);
831831

832832
char *argv1[] = {
833-
"ut",
834-
"--host=127.0.0.1",
835-
"--socket=/some/socket/path",
833+
const_cast<char *>("ut"),
834+
const_cast<char *>("--host=127.0.0.1"),
835+
const_cast<char *>("--socket=/some/socket/path"),
836836
NULL
837837
};
838-
test_conflicting_options("--host --socket", 3, argv0, error);
838+
test_conflicting_options("--host --socket", 3, argv1, error);
839839

840840
}
841841

@@ -845,8 +845,8 @@ TEST_F(Shell_cmdline_options, conflicts_port) {
845845

846846
char uri[] = {"--uri=mysqlx://root:password@localhost:3307"};
847847
char *argv0[] = {
848-
"ut",
849-
"--port=3306",
848+
const_cast<char *>("ut"),
849+
const_cast<char *>("--port=3306"),
850850
uri,
851851
NULL
852852
};
@@ -859,9 +859,9 @@ TEST_F(Shell_cmdline_options, conflicts_socket) {
859859
"socket in the URI.\n";
860860

861861
char *argv0[] = {
862-
"ut",
863-
"--socket=/path/to/socket",
864-
"--uri=mysqlx://root@/socket",
862+
const_cast<char *>("ut"),
863+
const_cast<char *>("--socket=/path/to/socket"),
864+
const_cast<char *>("--uri=mysqlx://root@/socket"),
865865
NULL
866866
};
867867

@@ -874,9 +874,9 @@ TEST_F(Shell_cmdline_options, conflicting_port_and_socket) {
874874
"together.\n";
875875

876876
char *argv0[] = {
877-
"ut",
878-
"--port=3307",
879-
"--socket=/some/weird/path",
877+
const_cast<char *>("ut"),
878+
const_cast<char *>("--port=3307"),
879+
const_cast<char *>("--socket=/some/weird/path"),
880880
NULL
881881
};
882882

@@ -886,9 +886,9 @@ TEST_F(Shell_cmdline_options, conflicting_port_and_socket) {
886886
"contains a socket.\n";
887887

888888
char *argv1[] = {
889-
"ut",
890-
"--uri=root@/socket",
891-
"--port=3306",
889+
const_cast<char *>("ut"),
890+
const_cast<char *>("--uri=root@/socket"),
891+
const_cast<char *>("--port=3306"),
892892
NULL
893893
};
894894

@@ -898,9 +898,9 @@ TEST_F(Shell_cmdline_options, conflicting_port_and_socket) {
898898
"contains a port.\n";
899899

900900
char *argv2[] = {
901-
"ut",
902-
"--uri=root@localhost:3306",
903-
"--socket=/some/socket/path",
901+
const_cast<char *>("ut"),
902+
const_cast<char *>("--uri=root@localhost:3306"),
903+
const_cast<char *>("--socket=/some/socket/path"),
904904
NULL
905905
};
906906

@@ -969,7 +969,11 @@ TEST_F(Shell_cmdline_options, test_uri_with_password) {
969969
TEST_F(Shell_cmdline_options, test_deprecated_ssl) {
970970
{
971971
std::string error = "The --ssl option is deprecated, use --ssl-mode instead";
972-
std::vector<char *> options = {"ut", "--ssl", "something", NULL};
972+
std::vector<char *> options = {
973+
const_cast<char *>("ut"),
974+
const_cast<char *>("--ssl"),
975+
const_cast<char *>("something"),
976+
NULL};
973977
test_deprecated_ssl("--ssl=something", options, error, 1,
974978
shcore::SslMode::Preferred); // This last param is
975979
// ignored on this case
@@ -978,36 +982,47 @@ TEST_F(Shell_cmdline_options, test_deprecated_ssl) {
978982
std::string wrequired = "WARNING: the --ssl option is deprecated, "
979983
"using --ssl-mode=REQUIRED";
980984
{
981-
std::vector<char *> options = {"ut", "--ssl", NULL};
985+
std::vector<char *> options = {
986+
const_cast<char *>("ut"),
987+
const_cast<char *>("--ssl"),
988+
NULL};
982989
test_deprecated_ssl("--ssl", options, wrequired, 0,
983990
shcore::SslMode::Required);
984991
}
985992
{
986-
std::vector<char *> options = {"ut", "--ssl=1", NULL};
993+
std::vector<char *> options = {
994+
const_cast<char *>("ut"),
995+
const_cast<char *>("--ssl=1"),
996+
NULL};
987997
test_deprecated_ssl("--ssl=1", options, wrequired, 0,
988998
shcore::SslMode::Required);
989999
}
9901000
{
991-
std::vector<char *> options = {"ut", "--ssl=yes", NULL};
1001+
std::vector<char *> options = {
1002+
const_cast<char *>("ut"),
1003+
const_cast<char *>("--ssl=yes"),
1004+
NULL};
9921005
test_deprecated_ssl("--ssl=yes", options, wrequired, 0,
9931006
shcore::SslMode::Required);
9941007
}
9951008

9961009
std::string wdisabled = "WARNING: the --ssl option is deprecated, "
9971010
"using --ssl-mode=DISABLED";
9981011
{
999-
std::vector<char *> options = {"ut", "--ssl=0", NULL};
1012+
std::vector<char *> options = {
1013+
const_cast<char *>("ut"),
1014+
const_cast<char *>("--ssl=0"),
1015+
NULL};
10001016
test_deprecated_ssl("--ssl=0", options, wdisabled, 0,
10011017
shcore::SslMode::Disabled);
10021018
}
10031019
{
1004-
std::vector<char *> options = {"ut", "--ssl=no", NULL};
1020+
std::vector<char *> options = {
1021+
const_cast<char *>("ut"),
1022+
const_cast<char *>("--ssl=no"),
1023+
NULL};
10051024
test_deprecated_ssl("--ssl=no", options, wdisabled, 0,
10061025
shcore::SslMode::Disabled);
10071026
}
10081027
}
1009-
10101028
}
1011-
1012-
1013-

0 commit comments

Comments
 (0)