Put strncpy in parens to silence GCC warning

GCC warns (-Wstringop-truncation) because the destination
buffer size is identical to max-size, leading to a potential
char[] with no null terminator.  nanopb can handle it fine,
and parantheses around strncpy silence that compiler warning.

nanopb treatment of string buffers with specific max length
seems dangerous in general, specifically when read from,
but this particular case should be correct, as the buffer
is only ever read by nanopb itself, which takes the max
size into consideration, in addition to the null terminator.
pull/19198/head
Mehrdad Afshari 6 years ago
parent 32a37e235a
commit 8fa95462a1
  1. 8
      src/core/ext/filters/client_channel/lb_policy/grpclb/load_balancer_api.cc
  2. 8
      src/core/ext/filters/client_channel/lb_policy/xds/xds_load_balancer_api.cc

@ -67,8 +67,12 @@ grpc_grpclb_request* grpc_grpclb_request_create(const char* lb_service_name) {
req->has_client_stats = false;
req->has_initial_request = true;
req->initial_request.has_name = true;
strncpy(req->initial_request.name, lb_service_name,
GRPC_GRPCLB_SERVICE_NAME_MAX_LENGTH);
// GCC warns (-Wstringop-truncation) because the destination
// buffer size is identical to max-size, leading to a potential
// char[] with no null terminator. nanopb can handle it fine,
// and parantheses around strncpy silence that compiler warning.
(strncpy(req->initial_request.name, lb_service_name,
GRPC_GRPCLB_SERVICE_NAME_MAX_LENGTH));
return req;
}

@ -67,8 +67,12 @@ xds_grpclb_request* xds_grpclb_request_create(const char* lb_service_name) {
req->has_client_stats = false;
req->has_initial_request = true;
req->initial_request.has_name = true;
strncpy(req->initial_request.name, lb_service_name,
XDS_SERVICE_NAME_MAX_LENGTH);
// GCC warns (-Wstringop-truncation) because the destination
// buffer size is identical to max-size, leading to a potential
// char[] with no null terminator. nanopb can handle it fine,
// and parantheses around strncpy silence that compiler warning.
(strncpy(req->initial_request.name, lb_service_name,
XDS_SERVICE_NAME_MAX_LENGTH));
return req;
}

Loading…
Cancel
Save