diff options
author | 2017-10-26 11:31:58 -0700 | |
---|---|---|
committer | 2017-10-26 11:31:58 -0700 | |
commit | a28214628b76159f33e76c49ad5e4d5aa96be5bf (patch) | |
tree | 67172c6fb521f0405408ed5e07e3633a0b067f30 /src/core | |
parent | 76d0ec4a73ffa662312732bc6514430acc207feb (diff) |
When parsing durations, handle any number of decimal digits up to 9.
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/ext/filters/client_channel/client_channel.cc | 24 |
1 files changed, 8 insertions, 16 deletions
diff --git a/src/core/ext/filters/client_channel/client_channel.cc b/src/core/ext/filters/client_channel/client_channel.cc index 49a87c77b9..d746f0e7d4 100644 --- a/src/core/ext/filters/client_channel/client_channel.cc +++ b/src/core/ext/filters/client_channel/client_channel.cc @@ -121,24 +121,16 @@ static bool parse_timeout(grpc_json *field, grpc_millis *timeout) { gpr_free(buf); return false; } - // There should always be exactly 3, 6, or 9 fractional digits. - int multiplier = 1; - switch (strlen(decimal_point + 1)) { - case 9: - break; - case 6: - multiplier *= 1000; - break; - case 3: - multiplier *= 1000000; - break; - default: // Unsupported number of digits. - gpr_free(buf); - return false; + int num_digits = (int)strlen(decimal_point + 1); + if (num_digits > 9) { // We don't accept greater precision than nanos. + gpr_free(buf); + return false; + } + for (int i = 0; i < (9 - num_digits); ++i) { + nanos *= 10; } - nanos *= multiplier; } - int seconds = gpr_parse_nonnegative_int(buf); + int seconds = decimal_point == buf ? 0 : gpr_parse_nonnegative_int(buf); gpr_free(buf); if (seconds == -1) return false; *timeout = seconds * GPR_MS_PER_SEC + nanos / GPR_NS_PER_MS; |