Skip to content

Commit 9d074f8

Browse files
committed
Getting Strings
1. Rewrite GetString() and GetStringAlloc() in terms of GetStringRef(). 2. Change GetStringRef() to allow for 0 length strings. 3. Update DoChannelRequest() to use GetStringAlloc() when reading the modes from the packet.
1 parent da85e49 commit 9d074f8

File tree

1 file changed

+31
-41
lines changed

1 file changed

+31
-41
lines changed

src/internal.c

Lines changed: 31 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3330,20 +3330,13 @@ int GetString(char* s, word32* sSz, const byte* buf, word32 len, word32 *idx)
33303330
{
33313331
int result;
33323332
word32 strSz;
3333+
const byte* str;
33333334

3334-
result = GetUint32(&strSz, buf, len, idx);
3335-
3335+
result = GetStringRef(&strSz, &str, buf, len, idx);
33363336
if (result == WS_SUCCESS) {
3337-
result = WS_BUFFER_E;
3338-
3339-
/* This allows 0 length string to be decoded */
3340-
if (*idx <= len && strSz <= len - *idx) {
3341-
*sSz = (strSz >= *sSz) ? *sSz - 1 : strSz; /* -1 for null char */
3342-
WMEMCPY(s, buf + *idx, *sSz);
3343-
*idx += strSz;
3344-
s[*sSz] = 0;
3345-
result = WS_SUCCESS;
3346-
}
3337+
*sSz = (strSz >= *sSz) ? *sSz - 1 : strSz; /* -1 for null char */
3338+
WMEMCPY(s, str, *sSz);
3339+
s[*sSz] = 0;
33473340
}
33483341

33493342
return result;
@@ -3355,24 +3348,24 @@ int GetString(char* s, word32* sSz, const byte* buf, word32 len, word32 *idx)
33553348
int GetStringAlloc(void* heap, char** s, const byte* buf, word32 len, word32 *idx)
33563349
{
33573350
int result;
3358-
char* str;
3351+
const byte *str;
33593352
word32 strSz;
33603353

3361-
result = GetUint32(&strSz, buf, len, idx);
3362-
3354+
result = GetStringRef(&strSz, &str, buf, len, idx);
33633355
if (result == WS_SUCCESS) {
3364-
if (*idx >= len || strSz > len - *idx)
3365-
return WS_BUFFER_E;
3366-
str = (char*)WMALLOC(strSz + 1, heap, DYNTYPE_STRING);
3367-
if (str == NULL)
3356+
char* newStr;
3357+
3358+
newStr = (char*)WMALLOC(strSz + 1, heap, DYNTYPE_STRING);
3359+
if (newStr == NULL)
33683360
return WS_MEMORY_E;
3369-
WMEMCPY(str, buf + *idx, strSz);
3370-
*idx += strSz;
3371-
str[strSz] = '\0';
3361+
3362+
if (strSz > 0 && str)
3363+
WMEMCPY(newStr, str, strSz);
3364+
newStr[strSz] = 0;
33723365

33733366
if (*s != NULL)
33743367
WFREE(*s, heap, DYNTYPE_STRING);
3375-
*s = str;
3368+
*s = newStr;
33763369
}
33773370

33783371
return result;
@@ -3387,15 +3380,17 @@ int GetStringRef(word32* strSz, const byte** str,
33873380
int result;
33883381

33893382
result = GetUint32(strSz, buf, len, idx);
3390-
33913383
if (result == WS_SUCCESS) {
3392-
result = WS_BUFFER_E;
3393-
3394-
if (*idx < len && *strSz <= len - *idx) {
3395-
*str = buf + *idx;
3396-
*idx += *strSz;
3397-
result = WS_SUCCESS;
3384+
if (*idx <= len && *strSz <= len - *idx) {
3385+
if (*strSz) {
3386+
*str = buf + *idx;
3387+
*idx += *strSz;
3388+
}
3389+
else
3390+
*str = NULL;
33983391
}
3392+
else
3393+
result = WS_BUFFER_E;
33993394
}
34003395

34013396
return result;
@@ -8812,8 +8807,8 @@ static int DoChannelRequest(WOLFSSH* ssh,
88128807
#ifdef WOLFSSH_TERM
88138808
else if (WSTRNCMP(type, "pty-req", typeSz) == 0) {
88148809
char term[32];
8815-
const byte* modes;
8816-
word32 termSz, modesSz = 0;
8810+
char* modes;
8811+
word32 termSz, modesSz;
88178812
word32 widthChar, heightRows, widthPixels, heightPixels;
88188813

88198814
termSz = (word32)sizeof(term);
@@ -8827,25 +8822,20 @@ static int DoChannelRequest(WOLFSSH* ssh,
88278822
if (ret == WS_SUCCESS)
88288823
ret = GetUint32(&heightPixels, buf, len, &begin);
88298824
if (ret == WS_SUCCESS)
8830-
ret = GetStringRef(&modesSz, &modes, buf, len, &begin);
8825+
ret = GetStringAlloc(&modesSz, &modes, buf, len, &begin);
88318826
if (ret == WS_SUCCESS) {
8832-
ssh->modes = (byte*)WMALLOC(modesSz,
8833-
ssh->ctx->heap, DYNTYPE_STRING);
8834-
if (ssh->modes == NULL)
8835-
ret = WS_MEMORY_E;
8836-
}
8837-
if (ret == WS_SUCCESS) {
8838-
ssh->modesSz = modesSz;
8839-
WMEMCPY(ssh->modes, modes, modesSz);
88408827
WLOG(WS_LOG_DEBUG, " term = %s", term);
88418828
WLOG(WS_LOG_DEBUG, " widthChar = %u", widthChar);
88428829
WLOG(WS_LOG_DEBUG, " heightRows = %u", heightRows);
88438830
WLOG(WS_LOG_DEBUG, " widthPixels = %u", widthPixels);
88448831
WLOG(WS_LOG_DEBUG, " heightPixels = %u", heightPixels);
8832+
WLOG(WS_LOG_DEBUG, " modesSz = %u", modesSz);
88458833
ssh->widthChar = widthChar;
88468834
ssh->heightRows = heightRows;
88478835
ssh->widthPixels = widthPixels;
88488836
ssh->heightPixels = heightPixels;
8837+
ssh->modes = (byte*)modes;
8838+
ssh->modesSz = modesSz;
88498839
if (ssh->termResizeCb) {
88508840
if (ssh->termResizeCb(ssh, widthChar, heightRows,
88518841
widthPixels, heightPixels,

0 commit comments

Comments
 (0)