GCC-8 string fixes

1. strncpy needs to include the source string's NULL.
2. Deleted a few redundant string modifications.
This commit is contained in:
John Safranek
2018-06-21 10:09:26 -07:00
parent ed208efc4d
commit af89458af0
5 changed files with 60 additions and 62 deletions

View File

@@ -1045,8 +1045,7 @@ int LoadCRL(WOLFSSL_CRL* crl, const char* path, int type, int monitor)
pathLen = (word32)XSTRLEN(path);
pathBuf = (char*)XMALLOC(pathLen+1, crl->heap,DYNAMIC_TYPE_CRL_MONITOR);
if (pathBuf) {
XSTRNCPY(pathBuf, path, pathLen);
pathBuf[pathLen] = '\0'; /* Null Terminate */
XSTRNCPY(pathBuf, path, pathLen+1);
if (type == WOLFSSL_FILETYPE_PEM) {
/* free old path before setting a new one */

View File

@@ -252,7 +252,8 @@ static const char* const msgTable[] =
/* *nix version uses table above */
static void GetError(int idx, char* str)
{
XSTRNCPY(str, msgTable[idx - 1], MAX_ERROR_LEN);
XSTRNCPY(str, msgTable[idx - 1], MAX_ERROR_LEN-1);
str[MAX_ERROR_LEN-1] = '\0';
}

View File

@@ -10733,8 +10733,8 @@ int wolfSSL_set_compression(WOLFSSL* ssl)
ssl->arrays->server_hint[0] = 0;
else {
XSTRNCPY(ssl->arrays->server_hint, hint,
sizeof(ssl->arrays->server_hint));
ssl->arrays->server_hint[MAX_PSK_ID_LEN] = '\0'; /* null term */
sizeof(ssl->arrays->server_hint)-1);
ssl->arrays->server_hint[sizeof(ssl->arrays->server_hint)-1] = '\0';
}
return WOLFSSL_SUCCESS;
}
@@ -16513,7 +16513,7 @@ WOLFSSL_EVP_PKEY* wolfSSL_X509_get_pubkey(WOLFSSL_X509* x509)
tmp[sizeof(tmp) - 1] = '\0';
if (mp_leading_bit(&rsa.n)) {
lbit = 1;
XSTRNCAT(tmp, "00", sizeof("00"));
XSTRNCAT(tmp, "00", 3);
}
rawLen = mp_unsigned_bin_size(&rsa.n);
@@ -25714,7 +25714,7 @@ static int EncryptDerKey(byte *der, int *derSz, const EVP_CIPHER* cipher,
return WOLFSSL_FAILURE;
}
XSTRNCPY((char*)*cipherInfo, info->name, cipherInfoSz);
XSTRNCAT((char*)*cipherInfo, ",", 1);
XSTRNCAT((char*)*cipherInfo, ",", 2);
idx = (word32)XSTRLEN((char*)*cipherInfo);
cipherInfoSz -= idx;

View File

@@ -1185,33 +1185,33 @@ int wolfIO_HttpBuildRequest(const char* reqType, const char* domainName,
if (maxLen > (word32)bufSize)
return 0;
XSTRNCPY((char*)buf, reqType, reqTypeLen);
buf += reqTypeLen;
XSTRNCPY((char*)buf, blankStr, blankStrLen+1);
buf += blankStrLen;
XSTRNCPY((char*)buf, path, pathLen);
buf += pathLen;
XSTRNCPY((char*)buf, http11Str, http11StrLen+1);
buf += http11StrLen;
XSTRNCPY((char*)buf, reqType, bufSize);
buf += reqTypeLen; bufSize -= reqTypeLen;
XSTRNCPY((char*)buf, blankStr, bufSize);
buf += blankStrLen; bufSize -= blankStrLen;
XSTRNCPY((char*)buf, path, bufSize);
buf += pathLen; bufSize -= pathLen;
XSTRNCPY((char*)buf, http11Str, bufSize);
buf += http11StrLen; bufSize -= http11StrLen;
if (domainNameLen > 0) {
XSTRNCPY((char*)buf, hostStr, hostStrLen+1);
buf += hostStrLen;
XSTRNCPY((char*)buf, domainName, domainNameLen);
buf += domainNameLen;
XSTRNCPY((char*)buf, hostStr, bufSize);
buf += hostStrLen; bufSize -= hostStrLen;
XSTRNCPY((char*)buf, domainName, bufSize);
buf += domainNameLen; bufSize -= domainNameLen;
}
if (reqSz > 0 && reqSzStrLen > 0) {
XSTRNCPY((char*)buf, contentLenStr, contentLenStrLen+1);
buf += contentLenStrLen;
XSTRNCPY((char*)buf, reqSzStr, reqSzStrLen);
buf += reqSzStrLen;
XSTRNCPY((char*)buf, contentLenStr, bufSize);
buf += contentLenStrLen; bufSize -= contentLenStrLen;
XSTRNCPY((char*)buf, reqSzStr, bufSize);
buf += reqSzStrLen; bufSize -= reqSzStrLen;
}
if (contentTypeLen > 0) {
XSTRNCPY((char*)buf, contentTypeStr, contentTypeStrLen+1);
buf += contentTypeStrLen;
XSTRNCPY((char*)buf, contentType, contentTypeLen);
buf += contentTypeLen;
XSTRNCPY((char*)buf, contentTypeStr, bufSize);
buf += contentTypeStrLen; bufSize -= contentTypeStrLen;
XSTRNCPY((char*)buf, contentType, bufSize);
buf += contentTypeLen; bufSize -= contentTypeLen;
}
XSTRNCPY((char*)buf, doubleCrLfStr, doubleCrLfStrLen+1);
XSTRNCPY((char*)buf, doubleCrLfStr, bufSize);
buf += doubleCrLfStrLen;
#ifdef WOLFIO_DEBUG

View File

@@ -4564,24 +4564,23 @@ int GetTimeString(byte* date, int format, char* buf, int len)
/* place month in buffer */
buf[0] = '\0';
switch(t.tm_mon) {
case 0: XSTRNCAT(buf, "Jan ", 4); break;
case 1: XSTRNCAT(buf, "Feb ", 4); break;
case 2: XSTRNCAT(buf, "Mar ", 4); break;
case 3: XSTRNCAT(buf, "Apr ", 4); break;
case 4: XSTRNCAT(buf, "May ", 4); break;
case 5: XSTRNCAT(buf, "Jun ", 4); break;
case 6: XSTRNCAT(buf, "Jul ", 4); break;
case 7: XSTRNCAT(buf, "Aug ", 4); break;
case 8: XSTRNCAT(buf, "Sep ", 4); break;
case 9: XSTRNCAT(buf, "Oct ", 4); break;
case 10: XSTRNCAT(buf, "Nov ", 4); break;
case 11: XSTRNCAT(buf, "Dec ", 4); break;
case 0: XSTRNCAT(buf, "Jan ", 5); break;
case 1: XSTRNCAT(buf, "Feb ", 5); break;
case 2: XSTRNCAT(buf, "Mar ", 5); break;
case 3: XSTRNCAT(buf, "Apr ", 5); break;
case 4: XSTRNCAT(buf, "May ", 5); break;
case 5: XSTRNCAT(buf, "Jun ", 5); break;
case 6: XSTRNCAT(buf, "Jul ", 5); break;
case 7: XSTRNCAT(buf, "Aug ", 5); break;
case 8: XSTRNCAT(buf, "Sep ", 5); break;
case 9: XSTRNCAT(buf, "Oct ", 5); break;
case 10: XSTRNCAT(buf, "Nov ", 5); break;
case 11: XSTRNCAT(buf, "Dec ", 5); break;
default:
return 0;
}
idx = 4; /* use idx now for char buffer */
buf[idx] = ' ';
XSNPRINTF(buf + idx, len - idx, "%2d %02d:%02d:%02d %d GMT",
t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec, t.tm_year + 1900);
@@ -7725,19 +7724,23 @@ static int wc_EncryptedInfoParse(EncryptedInfo* info,
#endif /* WOLFSSL_PEM_TO_DER */
#ifdef WOLFSSL_DER_TO_PEM
static int wc_EncryptedInfoAppend(char* dest, char* cipherInfo)
static int wc_EncryptedInfoAppend(char* dest, int destSz, char* cipherInfo)
{
if (cipherInfo != NULL) {
size_t cipherInfoStrLen = XSTRLEN(cipherInfo);
int cipherInfoStrLen = (int)XSTRLEN((char*)cipherInfo);
if (cipherInfoStrLen > HEADER_ENCRYPTED_KEY_SIZE - (9+14+10+3))
cipherInfoStrLen = HEADER_ENCRYPTED_KEY_SIZE - (9+14+10+3);
XSTRNCAT(dest, kProcTypeHeader, 9);
XSTRNCAT(dest, ": 4,ENCRYPTED\n", 14);
XSTRNCAT(dest, kDecInfoHeader, 8);
XSTRNCAT(dest, ": ", 2);
XSTRNCAT(dest, cipherInfo, cipherInfoStrLen);
XSTRNCAT(dest, "\n\n", 3);
if (destSz - (int)XSTRLEN(dest) >= cipherInfoStrLen + (9+14+8+2+2+1)) {
/* strncat's src length needs to include the NULL */
XSTRNCAT(dest, kProcTypeHeader, 10);
XSTRNCAT(dest, ": 4,ENCRYPTED\n", 15);
XSTRNCAT(dest, kDecInfoHeader, 9);
XSTRNCAT(dest, ": ", 3);
XSTRNCAT(dest, cipherInfo, destSz - (int)XSTRLEN(dest) - 1);
XSTRNCAT(dest, "\n\n", 4);
}
}
return 0;
}
@@ -7794,20 +7797,18 @@ int wc_DerToPemEx(const byte* der, word32 derSz, byte* output, word32 outSz,
}
#endif
/* null term and leave room for newline */
header[--headerLen] = '\0'; header[--headerLen] = '\0';
footer[--footerLen] = '\0'; footer[--footerLen] = '\0';
/* build header and footer based on type */
XSTRNCPY(header, headerStr, headerLen);
XSTRNCPY(footer, footerStr, footerLen);
XSTRNCPY(header, headerStr, headerLen - 1);
header[headerLen - 1] = 0;
XSTRNCPY(footer, footerStr, footerLen - 1);
footer[footerLen - 1] = 0;
/* add new line to end */
XSTRNCAT(header, "\n", 2);
XSTRNCAT(footer, "\n", 2);
#ifdef WOLFSSL_ENCRYPTED_KEYS
err = wc_EncryptedInfoAppend(header, (char*)cipher_info);
err = wc_EncryptedInfoAppend(header, headerLen, (char*)cipher_info);
if (err != 0) {
#ifdef WOLFSSL_SMALL_STACK
XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
@@ -9667,8 +9668,7 @@ static int EncodePolicyOID(byte *out, word32 *outSz, const char *in, void* heap)
if (str == NULL)
return MEMORY_E;
XSTRNCPY(str, in, len);
str[len] = '\0';
XSTRNCPY(str, in, len+1);
nb_val = 0;
@@ -11439,8 +11439,7 @@ int wc_SetKeyUsage(Cert *cert, const char *value)
if (str == NULL)
return MEMORY_E;
XSTRNCPY(str, value, len);
str[len] = '\0';
XSTRNCPY(str, value, len+1);
/* parse value, and set corresponding Key Usage value */
if ((token = XSTRTOK(str, ",", &ptr)) == NULL) {
@@ -11499,8 +11498,7 @@ int wc_SetExtKeyUsage(Cert *cert, const char *value)
if (str == NULL)
return MEMORY_E;
XSTRNCPY(str, value, len);
str[len] = '\0';
XSTRNCPY(str, value, len+1);
/* parse value, and set corresponding Key Usage value */
if ((token = XSTRTOK(str, ",", &ptr)) == NULL) {