Skip to content

Commit

Permalink
Change meaning of 'b' field values
Browse files Browse the repository at this point in the history
RabbitMQ prior to version 3.3.0 treated the 'b' field value as
unsigned; however, the specification and most clients treat it as
signed.
  • Loading branch information
squaremo committed Jun 25, 2015
1 parent 6157f75 commit 298d135
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
14 changes: 7 additions & 7 deletions lib/codec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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;
Expand Down
10 changes: 5 additions & 5 deletions test/codec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]],
Expand Down

0 comments on commit 298d135

Please sign in to comment.