Skip to content

Commit 97ccf07

Browse files
Eero KurimoJonathanHenson
authored andcommitted
Fix MSVC compiler warnings (aws#77)
* Explicitly cast int to uint16_t to satisfy MSVC compiler warnings * Use sprintf_s instead of sprintf in Windows to satisfy the MSVC compiler warning * Use snprintf instead of sprintf to satisfy the MSVC compiler warning * Apply clang formating to the mqtt_pub_sub sample
1 parent bef3faf commit 97ccf07

File tree

3 files changed

+19
-13
lines changed

3 files changed

+19
-13
lines changed

samples/mqtt_pub_sub/main.cpp

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818
#include <aws/iot/MqttClient.h>
1919

2020
#include <algorithm>
21+
#include <aws/crt/UUID.h>
2122
#include <condition_variable>
2223
#include <iostream>
2324
#include <mutex>
24-
#include <aws/crt/UUID.h>
2525

2626
using namespace Aws::Crt;
2727

@@ -34,7 +34,9 @@ static void s_printHelp()
3434
" --key <path to key> --topic --ca_file <optional: path to custom ca>"
3535
" --use_websocket --signing_region <region> --proxy_host <host> --proxy_port <port>\n\n");
3636
fprintf(stdout, "endpoint: the endpoint of the mqtt server not including a port\n");
37-
fprintf(stdout, "cert: path to your client certificate in PEM format. If this is not set you must specify use_websocket\n");
37+
fprintf(
38+
stdout,
39+
"cert: path to your client certificate in PEM format. If this is not set you must specify use_websocket\n");
3840
fprintf(stdout, "key: path to your key in PEM format. If this is not set you must specify use_websocket\n");
3941
fprintf(stdout, "topic: topic to publish, subscribe to.\n");
4042
fprintf(stdout, "client_id: client id to use (optional)\n");
@@ -44,9 +46,13 @@ static void s_printHelp()
4446
" in your trust store, set this.\n");
4547
fprintf(stdout, "\tIt's the path to a CA file in PEM format\n");
4648
fprintf(stdout, "use_websocket: if specified, uses a websocket over https (optional)\n");
47-
fprintf(stdout, "signing_region: used for websocket signer it should only be specific if websockets are used. (required for websockets)\n");
49+
fprintf(
50+
stdout,
51+
"signing_region: used for websocket signer it should only be specific if websockets are used. (required for "
52+
"websockets)\n");
4853
fprintf(stdout, "proxy_host: if you want to use a proxy with websockets, specify the host here (optional).\n");
49-
fprintf(stdout, "proxy_port: defaults to 8080 is proxy_host is set. Set this to any value you'd like (optional).\n\n");
54+
fprintf(
55+
stdout, "proxy_port: defaults to 8080 is proxy_host is set. Set this to any value you'd like (optional).\n\n");
5056
}
5157

5258
bool s_cmdOptionExists(char **begin, char **end, const String &option)
@@ -86,7 +92,7 @@ int main(int argc, char *argv[])
8692
bool useWebSocket = false;
8793

8894
/*********************** Parse Arguments ***************************/
89-
if (!(s_cmdOptionExists(argv, argv + argc, "--endpoint") && s_cmdOptionExists(argv, argv + argc, "--topic")))
95+
if (!(s_cmdOptionExists(argv, argv + argc, "--endpoint") && s_cmdOptionExists(argv, argv + argc, "--topic")))
9096
{
9197
s_printHelp();
9298
return 0;
@@ -129,7 +135,7 @@ int main(int argc, char *argv[])
129135

130136
if (s_cmdOptionExists(argv, argv + argc, "--proxy_port"))
131137
{
132-
proxyPort = atoi(s_getCmdOption(argv, argv + argc, "--proxy_port"));
138+
proxyPort = static_cast<uint16_t>(atoi(s_getCmdOption(argv, argv + argc, "--proxy_port")));
133139
}
134140
}
135141

@@ -159,8 +165,7 @@ int main(int argc, char *argv[])
159165

160166
if (!certificatePath.empty() && !keyPath.empty())
161167
{
162-
builder =
163-
Aws::Iot::MqttClientConnectionConfigBuilder(certificatePath.c_str(), keyPath.c_str());
168+
builder = Aws::Iot::MqttClientConnectionConfigBuilder(certificatePath.c_str(), keyPath.c_str());
164169
}
165170
else if (useWebSocket)
166171
{
@@ -279,9 +284,9 @@ int main(int argc, char *argv[])
279284
connection->OnConnectionResumed = std::move(onResumed);
280285

281286
connection->SetOnMessageHandler([](Mqtt::MqttConnection &, const String &topic, const ByteBuf &payload) {
282-
fprintf(stdout, "Generic Publish received on topic %s, payload:\n", topic.c_str());
283-
fwrite(payload.buffer, 1, payload.len, stdout);
284-
fprintf(stdout, "\n");
287+
fprintf(stdout, "Generic Publish received on topic %s, payload:\n", topic.c_str());
288+
fwrite(payload.buffer, 1, payload.len, stdout);
289+
fprintf(stdout, "\n");
285290
});
286291

287292
/*

samples/raw_mqtt_pub_sub/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ int main(int argc, char *argv[])
136136

137137
if (s_cmdOptionExists(argv, argv + argc, "--proxy_port"))
138138
{
139-
proxyPort = atoi(s_getCmdOption(argv, argv + argc, "--proxy_port"));
139+
proxyPort = static_cast<uint16_t>(atoi(s_getCmdOption(argv, argv + argc, "--proxy_port")));
140140
}
141141
}
142142

tests/IotServiceTest.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* express or implied. See the License for the specific language governing
1313
* permissions and limitations under the License.
1414
*/
15+
1516
#include <aws/crt/Api.h>
1617

1718
#include <aws/testing/aws_test_harness.h>
@@ -118,7 +119,7 @@ static int s_TestIotPublishSubscribe(Aws::Crt::Allocator *allocator, void *ctx)
118119
mqttConnection->OnConnectionCompleted = onConnectionCompleted;
119120
mqttConnection->OnDisconnect = onDisconnect;
120121
char clientId[32];
121-
sprintf(clientId, "aws-crt-cpp-v2-%d", tries);
122+
snprintf(clientId, sizeof(clientId), "aws-crt-cpp-v2-%d", tries);
122123
mqttConnection->Connect(clientId, true);
123124

124125
{

0 commit comments

Comments
 (0)