From 9aa4b91cbac1a60d1e4ddef407eff7f309470f62 Mon Sep 17 00:00:00 2001 From: gitsrc Date: Tue, 16 Oct 2018 22:47:22 +0800 Subject: [PATCH] fix: hexdec conversion exception for negative hex --- src/Jaeger/Propagator/JaegerPropagator.php | 4 +-- src/Jaeger/SpanContext.php | 30 +++++++++++++++++----- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/Jaeger/Propagator/JaegerPropagator.php b/src/Jaeger/Propagator/JaegerPropagator.php index 35a052a..fcf9de3 100644 --- a/src/Jaeger/Propagator/JaegerPropagator.php +++ b/src/Jaeger/Propagator/JaegerPropagator.php @@ -30,8 +30,8 @@ public function extract($format, $carrier){ if($k == Constants\Tracer_State_Header_Name){ list($traceId, $spanId, $parentId,$flags) = explode(':', $carrier[strtoupper($k)]); - $spanContext->spanId = hexdec($spanId); - $spanContext->parentId = hexdec($parentId); + $spanContext->spanId = $spanContext->hexToSignedInt($spanId); + $spanContext->parentId = $spanContext->hexToSignedInt($parentId); $spanContext->flags = $flags; $spanContext->traceIdToString($traceId); diff --git a/src/Jaeger/SpanContext.php b/src/Jaeger/SpanContext.php index 17c4be8..38e1884 100644 --- a/src/Jaeger/SpanContext.php +++ b/src/Jaeger/SpanContext.php @@ -94,13 +94,31 @@ public function isSampled(){ } - public function traceIdToString($traceId){ + public function hexToSignedInt($hex) + { + $hexToDecNum = hexdec($hex); + if (gettype($hexToDecNum) === "integer"){ + return $hexToDecNum; + } + $dec = 0; + $len = strlen($hex); + for ($i = 1; $i <= $len; $i++) { + $dec = bcadd($dec, bcmul(strval(hexdec($hex[$i - 1])), bcpow('16', strval($len - $i)))); + } + $bigUnsignedNumStr = $dec; + $bigsub = bcsub($bigUnsignedNumStr, bcadd(PHP_INT_MAX . '', 1)); + $bigAdd = bcadd($bigsub, PHP_INT_MIN . ''); + return intval($bigAdd); + } + + public function traceIdToString($traceId) + { $len = strlen($traceId); - if($len > 16){ - $this->traceIdHigh = hexdec(substr($traceId, 0, 16)); - $this->traceIdLow = hexdec(substr($traceId, 16)); - }else{ - $this->traceIdLow = hexdec($traceId); + if ($len > 16) { + $this->traceIdHigh = $this->hexToSignedInt(substr($traceId, 0, 16)); + $this->traceIdLow = $this->hexToSignedInt(substr($traceId, 16)); + } else { + $this->traceIdLow = $this->hexToSignedInt($traceId); } }