From 298d135a57f77df71bfed608326f248dd464322b Mon Sep 17 00:00:00 2001 From: Michael Bridgen Date: Wed, 24 Jun 2015 17:42:47 -0700 Subject: [PATCH] Change meaning of 'b' field values RabbitMQ prior to version 3.3.0 treated the 'b' field value as unsigned; however, the specification and most clients treat it as signed. --- lib/codec.js | 14 +++++++------- test/codec.js | 10 +++++----- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/lib/codec.js b/lib/codec.js index f62405db..8e27520b 100644 --- a/lib/codec.js +++ b/lib/codec.js @@ -156,13 +156,13 @@ function encodeFieldValue(buffer, value, offset) { buffer.writeDoubleBE(val, offset); offset += 8; } - else { // only signed values are used in tables by RabbitMQ, - // except for 'byte's which are only unsigned. (The - // errata on the RabbitMQ website is wrong on this -- - // see rabbit_binary_generator.erl) - if (val < 256 && val >= 0) { + else { // only signed values are used in tables by + // RabbitMQ. It *used* to (< v3.3.0) treat the byte 'b' + // type as unsigned, but most clients (and the spec) + // think it's signed, and now RabbitMQ does too. + if (val < 128 && val >= -128) { tag('b'); - buffer.writeUInt8(val, offset); offset++; + buffer.writeInt8(val, offset); offset++; } else if (val >= -0x8000 && val < 0x8000) { // short tag('s'); @@ -215,7 +215,7 @@ function decodeFields(slice) { var tag = String.fromCharCode(slice[offset]); offset++; switch (tag) { case 'b': - val = slice.readUInt8(offset); offset++; + val = slice.readInt8(offset); offset++; break; case 'S': len = slice.readUInt32BE(offset); offset += 4; diff --git a/test/codec.js b/test/codec.js index 7bdb72bb..9a9a56fb 100644 --- a/test/codec.js +++ b/test/codec.js @@ -13,11 +13,11 @@ var forAll = C.forAll; var testCases = [ // integers - ['byte', {byte: 127}, [4,98,121,116,101,98,127]], - ['byte max value', {byte: 255}, [4,98,121,116,101,98,255]], - ['byte min value', {byte: 0}, [4,98,121,116,101,98,0]], - ['< 0 promoted to signed short', {short: -1}, [5,115,104,111,114,116,115,255,255]], - ['> 255 promoted to short', {short: 256}, [5,115,104,111,114,116,115,1,0]], + ['byte', {byte: 112}, [4,98,121,116,101,98,112]], + ['byte max value', {byte: 127}, [4,98,121,116,101,98,127]], + ['byte min value', {byte: -128}, [4,98,121,116,101,98,128]], + ['< -128 promoted to signed short', {short: -129}, [5,115,104,111,114,116,115,255,127]], + ['> 127 promoted to short', {short: 128}, [5,115,104,111,114,116,115,0,128]], ['< 2^15 still a short', {short: 0x7fff}, [5,115,104,111,114,116,115,127,255]], ['-2^15 still a short', {short: -0x8000}, [5,115,104,111,114,116,115,128,0]], ['>= 2^15 promoted to int', {int: 0x8000}, [3,105,110,116,73,0,0,128,0]],