Skip to content

Commit db7492b

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 2a21844 commit db7492b

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
@@ -3426,20 +3426,13 @@ int GetString(char* s, word32* sSz, const byte* buf, word32 len, word32 *idx)
34263426
{
34273427
int result;
34283428
word32 strSz;
3429+
const byte* str;
34293430

3430-
result = GetUint32(&strSz, buf, len, idx);
3431-
3431+
result = GetStringRef(&strSz, &str, buf, len, idx);
34323432
if (result == WS_SUCCESS) {
3433-
result = WS_BUFFER_E;
3434-
3435-
/* This allows 0 length string to be decoded */
3436-
if (*idx <= len && strSz <= len - *idx) {
3437-
*sSz = (strSz >= *sSz) ? *sSz - 1 : strSz; /* -1 for null char */
3438-
WMEMCPY(s, buf + *idx, *sSz);
3439-
*idx += strSz;
3440-
s[*sSz] = 0;
3441-
result = WS_SUCCESS;
3442-
}
3433+
*sSz = (strSz >= *sSz) ? *sSz - 1 : strSz; /* -1 for null char */
3434+
WMEMCPY(s, str, *sSz);
3435+
s[*sSz] = 0;
34433436
}
34443437

34453438
return result;
@@ -3451,24 +3444,24 @@ int GetString(char* s, word32* sSz, const byte* buf, word32 len, word32 *idx)
34513444
int GetStringAlloc(void* heap, char** s, const byte* buf, word32 len, word32 *idx)
34523445
{
34533446
int result;
3454-
char* str;
3447+
const byte *str;
34553448
word32 strSz;
34563449

3457-
result = GetUint32(&strSz, buf, len, idx);
3458-
3450+
result = GetStringRef(&strSz, &str, buf, len, idx);
34593451
if (result == WS_SUCCESS) {
3460-
if (*idx >= len || strSz > len - *idx)
3461-
return WS_BUFFER_E;
3462-
str = (char*)WMALLOC(strSz + 1, heap, DYNTYPE_STRING);
3463-
if (str == NULL)
3452+
char* newStr;
3453+
3454+
newStr = (char*)WMALLOC(strSz + 1, heap, DYNTYPE_STRING);
3455+
if (newStr == NULL)
34643456
return WS_MEMORY_E;
3465-
WMEMCPY(str, buf + *idx, strSz);
3466-
*idx += strSz;
3467-
str[strSz] = '\0';
3457+
3458+
if (strSz > 0 && str)
3459+
WMEMCPY(newStr, str, strSz);
3460+
newStr[strSz] = 0;
34683461

34693462
if (*s != NULL)
34703463
WFREE(*s, heap, DYNTYPE_STRING);
3471-
*s = str;
3464+
*s = newStr;
34723465
}
34733466

34743467
return result;
@@ -3483,15 +3476,17 @@ int GetStringRef(word32* strSz, const byte** str,
34833476
int result;
34843477

34853478
result = GetUint32(strSz, buf, len, idx);
3486-
34873479
if (result == WS_SUCCESS) {
3488-
result = WS_BUFFER_E;
3489-
3490-
if (*idx < len && *strSz <= len - *idx) {
3491-
*str = buf + *idx;
3492-
*idx += *strSz;
3493-
result = WS_SUCCESS;
3480+
if (*idx <= len && *strSz <= len - *idx) {
3481+
if (*strSz) {
3482+
*str = buf + *idx;
3483+
*idx += *strSz;
3484+
}
3485+
else
3486+
*str = NULL;
34943487
}
3488+
else
3489+
result = WS_BUFFER_E;
34953490
}
34963491

34973492
return result;
@@ -8947,8 +8942,8 @@ static int DoChannelRequest(WOLFSSH* ssh,
89478942
#ifdef WOLFSSH_TERM
89488943
else if (WSTRNCMP(type, "pty-req", typeSz) == 0) {
89498944
char term[32];
8950-
const byte* modes;
8951-
word32 termSz, modesSz = 0;
8945+
char* modes;
8946+
word32 termSz, modesSz;
89528947
word32 widthChar, heightRows, widthPixels, heightPixels;
89538948

89548949
channel->ptyReq = 1; /* recieved a pty request */
@@ -8963,25 +8958,20 @@ static int DoChannelRequest(WOLFSSH* ssh,
89638958
if (ret == WS_SUCCESS)
89648959
ret = GetUint32(&heightPixels, buf, len, &begin);
89658960
if (ret == WS_SUCCESS)
8966-
ret = GetStringRef(&modesSz, &modes, buf, len, &begin);
8961+
ret = GetStringAlloc(&modesSz, &modes, buf, len, &begin);
89678962
if (ret == WS_SUCCESS) {
8968-
ssh->modes = (byte*)WMALLOC(modesSz,
8969-
ssh->ctx->heap, DYNTYPE_STRING);
8970-
if (ssh->modes == NULL)
8971-
ret = WS_MEMORY_E;
8972-
}
8973-
if (ret == WS_SUCCESS) {
8974-
ssh->modesSz = modesSz;
8975-
WMEMCPY(ssh->modes, modes, modesSz);
89768963
WLOG(WS_LOG_DEBUG, " term = %s", term);
89778964
WLOG(WS_LOG_DEBUG, " widthChar = %u", widthChar);
89788965
WLOG(WS_LOG_DEBUG, " heightRows = %u", heightRows);
89798966
WLOG(WS_LOG_DEBUG, " widthPixels = %u", widthPixels);
89808967
WLOG(WS_LOG_DEBUG, " heightPixels = %u", heightPixels);
8968+
WLOG(WS_LOG_DEBUG, " modesSz = %u", modesSz);
89818969
ssh->widthChar = widthChar;
89828970
ssh->heightRows = heightRows;
89838971
ssh->widthPixels = widthPixels;
89848972
ssh->heightPixels = heightPixels;
8973+
ssh->modes = (byte*)modes;
8974+
ssh->modesSz = modesSz;
89858975
if (ssh->termResizeCb) {
89868976
if (ssh->termResizeCb(ssh, widthChar, heightRows,
89878977
widthPixels, heightPixels,

0 commit comments

Comments
 (0)