Skip to content

Commit b5c9751

Browse files
authored
Merge pull request #857 from ejohnstown/getstrings
Getting Strings
2 parents f1b5b78 + be7784c commit b5c9751

File tree

1 file changed

+30
-40
lines changed

1 file changed

+30
-40
lines changed

src/internal.c

Lines changed: 30 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3584,20 +3584,13 @@ int GetString(char* s, word32* sSz, const byte* buf, word32 len, word32 *idx)
35843584
{
35853585
int result;
35863586
word32 strSz;
3587+
const byte* str;
35873588

3588-
result = GetUint32(&strSz, buf, len, idx);
3589-
3589+
result = GetStringRef(&strSz, &str, buf, len, idx);
35903590
if (result == WS_SUCCESS) {
3591-
result = WS_BUFFER_E;
3592-
3593-
/* This allows 0 length string to be decoded */
3594-
if (*idx <= len && strSz <= len - *idx) {
3595-
*sSz = (strSz >= *sSz) ? *sSz - 1 : strSz; /* -1 for null char */
3596-
WMEMCPY(s, buf + *idx, *sSz);
3597-
*idx += strSz;
3598-
s[*sSz] = 0;
3599-
result = WS_SUCCESS;
3600-
}
3591+
*sSz = (strSz >= *sSz) ? *sSz - 1 : strSz; /* -1 for null char */
3592+
WMEMCPY(s, str, *sSz);
3593+
s[*sSz] = 0;
36013594
}
36023595

36033596
return result;
@@ -3609,24 +3602,24 @@ int GetString(char* s, word32* sSz, const byte* buf, word32 len, word32 *idx)
36093602
int GetStringAlloc(void* heap, char** s, const byte* buf, word32 len, word32 *idx)
36103603
{
36113604
int result;
3612-
char* str;
3605+
const byte *str;
36133606
word32 strSz;
36143607

3615-
result = GetUint32(&strSz, buf, len, idx);
3616-
3608+
result = GetStringRef(&strSz, &str, buf, len, idx);
36173609
if (result == WS_SUCCESS) {
3618-
if (*idx >= len || strSz > len - *idx)
3619-
return WS_BUFFER_E;
3620-
str = (char*)WMALLOC(strSz + 1, heap, DYNTYPE_STRING);
3621-
if (str == NULL)
3610+
char* newStr;
3611+
3612+
newStr = (char*)WMALLOC(strSz + 1, heap, DYNTYPE_STRING);
3613+
if (newStr == NULL)
36223614
return WS_MEMORY_E;
3623-
WMEMCPY(str, buf + *idx, strSz);
3624-
*idx += strSz;
3625-
str[strSz] = '\0';
3615+
3616+
if (strSz > 0 && str)
3617+
WMEMCPY(newStr, str, strSz);
3618+
newStr[strSz] = 0;
36263619

36273620
if (*s != NULL)
36283621
WFREE(*s, heap, DYNTYPE_STRING);
3629-
*s = str;
3622+
*s = newStr;
36303623
}
36313624

36323625
return result;
@@ -3641,15 +3634,17 @@ int GetStringRef(word32* strSz, const byte** str,
36413634
int result;
36423635

36433636
result = GetUint32(strSz, buf, len, idx);
3644-
36453637
if (result == WS_SUCCESS) {
3646-
result = WS_BUFFER_E;
3647-
3648-
if (*idx < len && *strSz <= len - *idx) {
3649-
*str = buf + *idx;
3650-
*idx += *strSz;
3651-
result = WS_SUCCESS;
3638+
if (*idx <= len && *strSz <= len - *idx) {
3639+
if (*strSz) {
3640+
*str = buf + *idx;
3641+
*idx += *strSz;
3642+
}
3643+
else
3644+
*str = NULL;
36523645
}
3646+
else
3647+
result = WS_BUFFER_E;
36533648
}
36543649

36553650
return result;
@@ -9142,7 +9137,7 @@ static int DoChannelRequest(WOLFSSH* ssh,
91429137
#ifdef WOLFSSH_TERM
91439138
else if (WSTRNCMP(type, "pty-req", typeSz) == 0) {
91449139
char term[32];
9145-
const byte* modes;
9140+
char* modes = NULL;
91469141
word32 termSz, modesSz = 0;
91479142
word32 widthChar, heightRows, widthPixels, heightPixels;
91489143

@@ -9158,25 +9153,20 @@ static int DoChannelRequest(WOLFSSH* ssh,
91589153
if (ret == WS_SUCCESS)
91599154
ret = GetUint32(&heightPixels, buf, len, &begin);
91609155
if (ret == WS_SUCCESS)
9161-
ret = GetStringRef(&modesSz, &modes, buf, len, &begin);
9156+
ret = GetStringAlloc(&modesSz, &modes, buf, len, &begin);
91629157
if (ret == WS_SUCCESS) {
9163-
ssh->modes = (byte*)WMALLOC(modesSz,
9164-
ssh->ctx->heap, DYNTYPE_STRING);
9165-
if (ssh->modes == NULL)
9166-
ret = WS_MEMORY_E;
9167-
}
9168-
if (ret == WS_SUCCESS) {
9169-
ssh->modesSz = modesSz;
9170-
WMEMCPY(ssh->modes, modes, modesSz);
91719158
WLOG(WS_LOG_DEBUG, " term = %s", term);
91729159
WLOG(WS_LOG_DEBUG, " widthChar = %u", widthChar);
91739160
WLOG(WS_LOG_DEBUG, " heightRows = %u", heightRows);
91749161
WLOG(WS_LOG_DEBUG, " widthPixels = %u", widthPixels);
91759162
WLOG(WS_LOG_DEBUG, " heightPixels = %u", heightPixels);
9163+
WLOG(WS_LOG_DEBUG, " modesSz = %u", modesSz);
91769164
ssh->widthChar = widthChar;
91779165
ssh->heightRows = heightRows;
91789166
ssh->widthPixels = widthPixels;
91799167
ssh->heightPixels = heightPixels;
9168+
ssh->modes = (byte*)modes;
9169+
ssh->modesSz = modesSz;
91809170
if (ssh->termResizeCb) {
91819171
if (ssh->termResizeCb(ssh, widthChar, heightRows,
91829172
widthPixels, heightPixels,

0 commit comments

Comments
 (0)