fix: EncodePrivateSyscap outputLen check.

Change-Id: If099114c6137fffc181f66db2f868fe12083dfc8
Signed-off-by: yudechen <chenyude@huawei.com>
This commit is contained in:
yudechen
2022-08-15 15:33:46 +08:00
parent 76fb76f6fc
commit 45a3e6da5d
2 changed files with 15 additions and 15 deletions
+12 -12
View File
@@ -14,6 +14,7 @@
*/
#include <errno.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
@@ -160,21 +161,19 @@ bool EncodePrivateSyscap(char **output, int *outputLen)
return false;
}
*outputLen = bufferLen - PCID_MAIN_BYTES - 1;
if (*outputLen == 0) {
*output = outputStr;
(*outputLen)--;
return true;
}
outputStr = (char *)malloc(*outputLen);
if (outputStr == NULL) {
PRINT_ERR("malloc buffer failed, size = %d, errno = %d\n", *outputLen, errno);
int priLen = bufferLen - PCID_MAIN_BYTES - 1;
if (priLen <= 0) {
*outputLen = 0;
return false;
}
outputStr = (char *)calloc(priLen, sizeof(char));
if (outputStr == NULL) {
PRINT_ERR("malloc buffer failed, size = %d, errno = %d\n", priLen, errno);
*outputLen = 0;
return false;
}
(void)memset_s(outputStr, *outputLen, 0, *outputLen);
ret = strncpy_s(outputStr, *outputLen, contextBuffer + PCID_MAIN_BYTES, *outputLen - 1);
ret = strncpy_s(outputStr, priLen, contextBuffer + PCID_MAIN_BYTES, priLen - 1);
if (ret != 0) {
PRINT_ERR("strcpy_s failed.");
FreeContextBuffer(contextBuffer);
@@ -184,6 +183,7 @@ bool EncodePrivateSyscap(char **output, int *outputLen)
}
FreeContextBuffer(contextBuffer);
*outputLen = strlen(outputStr);
*output = outputStr;
return true;
}
@@ -247,7 +247,7 @@ bool DecodePrivateSyscap(char *input, char (**output)[SINGLE_SYSCAP_LEN], int *o
if (input == NULL) {
*output = outputArray;
*outputCnt = syscapCnt;
return true;
return false;
}
while (*inputPos != '\0') {