Skip to content

Commit ab71195

Browse files
author
Tim Bradgate
committed
MB-27661 [11/n]: Fix MSVC warnings - C4267
This patch addresses the following generated warnings: C4267 - var : conversion from 'size_t' to 'type', possible loss of data The compiler detected a conversion from size_t to a smaller type. This is the second and final commit addressing this error. Change-Id: If23806c43a2024df2d4103f1b40cbd790957f1f0 Reviewed-on: http://review.couchbase.org/90604 Reviewed-by: Dave Rigby <[email protected]> Tested-by: Build Bot <[email protected]>
1 parent 2ee60e0 commit ab71195

25 files changed

+128
-84
lines changed

cbsasl/sasl_server_test.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <string.h>
2525
#include <algorithm>
2626
#include <array>
27+
#include <gsl/gsl>
2728

2829
char envptr[1024]{"CBSASL_PWFILE=" SOURCE_ROOT "/cbsasl/sasl_server_test.json"};
2930

@@ -40,7 +41,7 @@ static int sasl_getopt_callback(void*,
4041

4142
if (strcmp(option, "sasl mechanisms") == 0) {
4243
*result = mechanisms.c_str();
43-
*len = mechanisms.length();
44+
*len = gsl::narrow<unsigned>(mechanisms.length());
4445
return CBSASL_OK;
4546
}
4647

cbsasl/scram-sha/scram-sha.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ static std::string hex_encode_nonce(const std::array<char, 8>& nonce) {
5656
*/
5757
static bool decodeAttributeList(cbsasl_conn_t& conn, const std::string& list,
5858
AttributeMap& attributes) {
59-
unsigned long pos = 0;
59+
size_t pos = 0;
6060

6161
logging::log(conn,
6262
logging::Level::Debug,
@@ -502,7 +502,7 @@ cbsasl_error_t ScramShaServerBackend::step(const char* input,
502502

503503
server_final_message = out.str();
504504
(*output) = server_final_message.data();
505-
(*outputlen) = server_final_message.length();
505+
(*outputlen) = gsl::narrow<unsigned>(server_final_message.length());
506506

507507
std::string clientproof = iter->second;
508508
std::string my_clientproof = Couchbase::Base64::encode(getClientProof());
@@ -713,7 +713,7 @@ cbsasl_error_t ScramShaClientBackend::step(const char* input,
713713
client_final_message = out.str();
714714

715715
*output = client_final_message.data();
716-
*outputlen = client_final_message.length();
716+
*outputlen = gsl::narrow<unsigned>(client_final_message.length());
717717
logging::log(conn, logging::Level::Trace, client_final_message);
718718
return CBSASL_CONTINUE;
719719
} else {

daemon/connection_mcbp.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include <utilities/protocol2text.h>
3434
#include <cctype>
3535
#include <exception>
36+
#include <gsl/gsl>
3637

3738
bool McbpConnection::unregisterEvent() {
3839
if (!registered_in_libevent) {

daemon/ssl_context_openssl.cc

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,11 @@ void SslContext::disable() {
134134
bool SslContext::drainInputSocketBuf() {
135135
if (!inputPipe.empty()) {
136136
auto* bio = network;
137-
auto n = inputPipe.consume(
138-
[bio](cb::const_byte_buffer data) -> ssize_t {
139-
return BIO_write(bio, data.data(), data.size());
140-
});
137+
auto n =
138+
inputPipe.consume([bio](cb::const_byte_buffer data) -> ssize_t {
139+
return BIO_write(
140+
bio, data.data(), gsl::narrow<int>(data.size()));
141+
});
141142

142143
if (n > 0) {
143144
// We did move some data
@@ -229,7 +230,8 @@ void SslContext::drainBioSendPipe(SOCKET sfd) {
229230
if (!outputPipe.full()) {
230231
auto* bio = network;
231232
auto n = outputPipe.produce([bio](cb::byte_buffer data) -> ssize_t {
232-
return BIO_read(bio, data.data(), data.size());
233+
return BIO_read(
234+
bio, data.data(), gsl::narrow<int>(data.size()));
233235
});
234236

235237
if (n > 0) {

daemon/statemachine_mcbp.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
#include <mcbp/mcbp.h>
2828
#include <platform/strerror.h>
29+
#include <gsl/gsl>
2930

3031
static bool conn_new_cmd(McbpConnection& connection);
3132
static bool conn_waiting(McbpConnection& connection);
@@ -372,7 +373,7 @@ bool conn_execute(McbpConnection& connection) {
372373
throw std::logic_error(
373374
"conn_execute: Not enough data in input buffer");
374375
}
375-
return size;
376+
return gsl::narrow<ssize_t>(size);
376377
});
377378

378379
return true;

daemon/stdin_check.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ std::function<void()> exit_function;
1919

2020
static char* get_command(char* buffer, size_t buffsize) {
2121
#ifdef WIN32
22-
if (fgets(buffer, buffsize, stdin) == NULL) {
22+
if (fgets(buffer, gsl::narrow<int>(buffsize), stdin) == NULL) {
2323
return NULL;
2424
}
2525
return buffer;

daemon/subdocument.cc

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include <memcached/types.h>
3838
#include <platform/histogram.h>
3939
#include <xattr/blob.h>
40+
#include <gsl/gsl>
4041

4142
static const std::array<SubdocCmdContext::Phase, 2> phases{{SubdocCmdContext::Phase::XATTR,
4243
SubdocCmdContext::Phase::Body}};
@@ -67,10 +68,14 @@ static ENGINE_ERROR_CODE subdoc_update(SubdocCmdContext& context,
6768
static void subdoc_response(Cookie& cookie, SubdocCmdContext& context);
6869

6970
// Debug - print details of the specified subdocument command.
70-
static void subdoc_print_command(Connection& c, protocol_binary_command cmd,
71-
const char* key, const uint16_t keylen,
72-
const char* path, const uint16_t pathlen,
73-
const char* value, const uint32_t vallen) {
71+
static void subdoc_print_command(Connection& c,
72+
protocol_binary_command cmd,
73+
const char* key,
74+
const uint16_t keylen,
75+
const char* path,
76+
const size_t pathlen,
77+
const char* value,
78+
const size_t vallen) {
7479
char clean_key[KEY_MAX_LENGTH + 32];
7580
char clean_path[SUBDOC_PATH_MAX_LENGTH];
7681
char clean_value[80]; // only print the first few characters of the value.
@@ -1406,7 +1411,8 @@ static size_t encode_multi_mutation_result_spec(uint8_t index,
14061411
// Also encode resultlen if status is success.
14071412
if (op.status == PROTOCOL_BINARY_RESPONSE_SUCCESS) {
14081413
const auto& mloc = op.result.matchloc();
1409-
*reinterpret_cast<uint32_t*>(cursor) = htonl(mloc.length);
1414+
*reinterpret_cast<uint32_t*>(cursor) =
1415+
htonl(gsl::narrow<uint32_t>(mloc.length));
14101416
cursor += sizeof(uint32_t);
14111417
}
14121418
return cursor - buffer;
@@ -1534,20 +1540,21 @@ static void subdoc_multi_mutation_response(Cookie& cookie,
15341540
}
15351541

15361542
// Allocated required resource - build the header.
1537-
mcbp_add_header(cookie,
1538-
status_code,
1539-
extlen,
1540-
/*keylen*/ 0,
1541-
extlen + response_buf_needed + iov_len,
1542-
PROTOCOL_BINARY_RAW_BYTES);
1543+
mcbp_add_header(
1544+
cookie,
1545+
status_code,
1546+
gsl::narrow<uint8_t>(extlen),
1547+
/*keylen*/ 0,
1548+
gsl::narrow<uint32_t>(extlen + response_buf_needed + iov_len),
1549+
PROTOCOL_BINARY_RAW_BYTES);
15431550

15441551
// Append extras if requested.
15451552
if (extlen > 0) {
15461553
connection.addIov(reinterpret_cast<void*>(extras_ptr), extlen);
15471554
}
15481555

15491556
// Append the iovecs for each operation result.
1550-
size_t index = 0;
1557+
uint8_t index = 0;
15511558
for (auto phase : phases) {
15521559
for (size_t ii = 0; ii < context.getOperations(phase).size(); ii++, index++) {
15531560
const auto& op = context.getOperations(phase)[ii];
@@ -1557,7 +1564,7 @@ static void subdoc_multi_mutation_response(Cookie& cookie,
15571564
if (op.traits.responseHasValue() && mloc.length > 0) {
15581565
char* header = response_buf.getCurrent();
15591566
size_t header_sz =
1560-
encode_multi_mutation_result_spec(index, op, header);
1567+
encode_multi_mutation_result_spec(index, op, header);
15611568

15621569
connection.addIov(reinterpret_cast<void*>(header),
15631570
header_sz);
@@ -1570,7 +1577,7 @@ static void subdoc_multi_mutation_response(Cookie& cookie,
15701577
if (op.status != PROTOCOL_BINARY_RESPONSE_SUCCESS) {
15711578
char* header = response_buf.getCurrent();
15721579
size_t header_sz =
1573-
encode_multi_mutation_result_spec(index, op, header);
1580+
encode_multi_mutation_result_spec(index, op, header);
15741581

15751582
connection.addIov(reinterpret_cast<void*>(header),
15761583
header_sz);
@@ -1641,7 +1648,7 @@ static void subdoc_multi_lookup_response(Cookie& cookie,
16411648
status_code,
16421649
/*extlen*/ 0, /*keylen*/
16431650
0,
1644-
context.response_val_len,
1651+
gsl::narrow<uint32_t>(context.response_val_len),
16451652
PROTOCOL_BINARY_RAW_BYTES);
16461653

16471654
// Append the iovecs for each operation result.

daemon/timing_histogram.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <atomic>
2525
#include <chrono>
2626
#include <functional>
27+
#include <gsl/gsl>
2728
#include <sstream>
2829
#include <string>
2930

@@ -161,7 +162,7 @@ std::string TimingHistogram::to_string(void) {
161162
size_t len = msec.size();
162163
// element 0 isn't used
163164
for (size_t ii = 1; ii < len; ii++) {
164-
cJSON *obj = cJSON_CreateNumber(get_msec(ii));
165+
cJSON* obj = cJSON_CreateNumber(get_msec(gsl::narrow<uint8_t>(ii)));
165166
cJSON_AddItemToArray(array, obj);
166167
}
167168
cJSON_AddItemToObject(root, "ms", array);

daemon/topkeys.cc

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@
1717

1818
#include "config.h"
1919

20+
#include <inttypes.h>
21+
#include <platform/platform.h>
22+
#include <stdlib.h>
23+
#include <sys/types.h>
2024
#include <algorithm>
2125
#include <cstring>
22-
#include <sys/types.h>
26+
#include <gsl/gsl>
2327
#include <stdexcept>
24-
#include <stdlib.h>
25-
#include <inttypes.h>
26-
#include <platform/platform.h>
2728

2829
#include "topkeys.h"
2930

@@ -221,7 +222,11 @@ static void tk_iterfunc(const std::string& key, const topkey_item_t& it,
221222
",atime=%" PRIu32, it.ti_access_count,
222223
created_time, created_time);
223224
if (vlen > 0 && vlen < int(sizeof(val_str) - 1)) {
224-
c->add_stat(key.c_str(), key.size(), val_str, vlen, c->cookie);
225+
c->add_stat(key.c_str(),
226+
gsl::narrow<uint16_t>(key.size()),
227+
val_str,
228+
vlen,
229+
c->cookie);
225230
}
226231
}
227232

engines/default_engine/items.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
22
#include "config.h"
3-
#include <fcntl.h>
43
#include <errno.h>
5-
#include <stdlib.h>
4+
#include <fcntl.h>
5+
#include <inttypes.h>
66
#include <stdio.h>
7+
#include <stdlib.h>
78
#include <string.h>
89
#include <time.h>
9-
#include <inttypes.h>
10+
#include <gsl/gsl>
1011

1112
#include <logger/logger.h>
1213
#include <memcached/server_api.h>
@@ -1231,8 +1232,7 @@ static bool hash_key_create(hash_key* hkey,
12311232
const size_t nkey,
12321233
struct default_engine* engine,
12331234
const void* cookie) {
1234-
1235-
int hash_key_len = sizeof(bucket_id_t) + nkey;
1235+
uint16_t hash_key_len = gsl::narrow<uint16_t>(sizeof(bucket_id_t) + nkey);
12361236
if (nkey > sizeof(hkey->key_storage.client_key)) {
12371237
hkey->header.full_key =
12381238
static_cast<hash_key_data*>(cb_malloc(hash_key_len));

0 commit comments

Comments
 (0)