Skip to content

Commit

Permalink
fix: hexdec conversion exception for negative hex
Browse files Browse the repository at this point in the history
  • Loading branch information
corerman committed Oct 16, 2018
1 parent 8e4e203 commit 9aa4b91
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/Jaeger/Propagator/JaegerPropagator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
30 changes: 24 additions & 6 deletions src/Jaeger/SpanContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down

0 comments on commit 9aa4b91

Please sign in to comment.