Skip to content

Commit 41c13f6

Browse files
ejohnstownpadelsbach
authored andcommitted
internal: store a decoded boolean as 0 or 1
GetBoolean() stores any nonzero wire byte as 1, per RFC 4251 section 5, so callers and callbacks such as hasSignature see only 0 or 1. - tests: add test_GetBoolean covering 0, 1 and other nonzero bytes Issue: F-12573
1 parent 0ff99ca commit 41c13f6

2 files changed

Lines changed: 34 additions & 1 deletion

File tree

‎src/internal.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5712,7 +5712,7 @@ int GetBoolean(byte* v, const byte* buf, word32 len, word32* idx)
57125712
int result = WS_BUFFER_E;
57135713

57145714
if (*idx < len) {
5715-
*v = buf[*idx];
5715+
*v = (buf[*idx] != 0);
57165716
*idx += BOOLEAN_SZ;
57175717
result = WS_SUCCESS;
57185718
}

‎tests/unit.c‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5096,6 +5096,35 @@ static int test_GetString_zeroDestSz(void)
50965096
return result;
50975097
}
50985098

5099+
/* GetBoolean() stores any nonzero wire byte as 1 (RFC 4251 5). */
5100+
static int test_GetBoolean(void)
5101+
{
5102+
static const byte wire[] = { 0x00, 0x01, 0x02, 0x80, 0xFF };
5103+
static const byte expect[] = { 0, 1, 1, 1, 1 };
5104+
word32 idx = 0;
5105+
word32 i;
5106+
byte v;
5107+
5108+
for (i = 0; i < (word32)sizeof(wire); i++) {
5109+
v = 0xAA;
5110+
if (GetBoolean(&v, wire, (word32)sizeof(wire), &idx) != WS_SUCCESS)
5111+
return -1;
5112+
if (v != expect[i])
5113+
return -2;
5114+
if (idx != i + 1)
5115+
return -3;
5116+
}
5117+
5118+
/* past the end */
5119+
v = 0xAA;
5120+
if (GetBoolean(&v, wire, (word32)sizeof(wire), &idx) != WS_BUFFER_E)
5121+
return -4;
5122+
if (v != 0xAA || idx != (word32)sizeof(wire))
5123+
return -5;
5124+
5125+
return 0;
5126+
}
5127+
50995128
/* DoChannelWindowAdjust adds the peer's advertised bytes to peerWindowSz.
51005129
* A crafted bytesToAdd that would wrap the word32 must be rejected with
51015130
* WS_OVERFLOW_E and leave the window untouched; a value that fits must be
@@ -24101,6 +24130,10 @@ int wolfSSH_UnitTest(int argc, char** argv)
2410124130
(unitResult == 0 ? "SUCCESS" : "FAILED"));
2410224131
testResult = testResult || unitResult;
2410324132

24133+
unitResult = test_GetBoolean();
24134+
printf("GetBoolean: %s\n", (unitResult == 0 ? "SUCCESS" : "FAILED"));
24135+
testResult = testResult || unitResult;
24136+
2410424137
unitResult = test_GetString_zeroDestSz();
2410524138
printf("GetString_zeroDestSz: %s\n",
2410624139
(unitResult == 0 ? "SUCCESS" : "FAILED"));

0 commit comments

Comments
 (0)