// if nEnforcedMaxLength is 10, then it will actually enforce a string at 9 length. // That is, up through index 8 (9th byte) instead of index 9 (10th byte.) This is because // we are assuming the buffer has no more room than 10 bytes, and thus index 9 (10th byte) // MUST be reserved for the null terminating '\0'. Therefore, if the string is actually 10 // bytes long, necessitating an 11th byte for the null terminator, then you should pass 11 // here, aka OTString::GetLength()+1. That way the entire string will fit. // void OTString::LowLevelSet(const char * new_string, uint32_t nEnforcedMaxLength) { OT_ASSERT(NULL == m_strBuffer); // otherwise memory leak. if (NULL != new_string) { uint32_t nLength = (nEnforcedMaxLength > 0) ? static_cast<uint32_t> (OTString::safe_strlen(new_string, static_cast<size_t>(nEnforcedMaxLength))) : static_cast<uint32_t> (OTString::safe_strlen(new_string, static_cast<size_t>(MAX_STRING_LENGTH-1))); // room for \0 // don't bother allocating memory for a 0 length string. if (0 == nLength) return; OT_ASSERT_MSG(nLength < (MAX_STRING_LENGTH-10), "ASSERT: OTString::LowLevelSet: Exceeded MAX_STRING_LENGTH! (String would not have fully fit anyway--it would have been truncated here, potentially causing data corruption.)"); // 10 being a buffer. // ------------------------------------------ // Add null terminator to source string JUST IN CASE... // Update: this is const, so we can't change it. However, the strnlen above will only have // worked if there was a null terminator, since otherwise we would have hit the above ASSERT. // Therefore we should be safe enough without it here... // // new_string[nLength] = '\0'; // ------------------------------------------ m_strBuffer = str_dup2(new_string, nLength); if (NULL != m_strBuffer) m_lLength = nLength; else m_lLength = 0; } }
void OTString::LowLevelSetStr(const OTString & strBuf) { if (strBuf.Exists()) { m_lLength = strBuf.m_lLength; m_strBuffer = str_dup2(strBuf.m_strBuffer, m_lLength); } }
void OTString::LowLevelSetStr(const OTString & strBuf) { OT_ASSERT(NULL == m_strBuffer); // otherwise memory leak. if (strBuf.Exists()) { m_lLength = (MAX_STRING_LENGTH > strBuf.m_lLength) ? strBuf.m_lLength : (MAX_STRING_LENGTH-1); m_strBuffer = str_dup2(strBuf.m_strBuffer, m_lLength); } }
void OTString::LowLevelSetStr(const OTString & strBuf) { OT_ASSERT(NULL == m_strBuffer); // otherwise memory leak. if (strBuf.Exists()) { m_lLength = (MAX_STRING_LENGTH > strBuf.m_lLength) ? strBuf.m_lLength : (MAX_STRING_LENGTH-1); OT_ASSERT_MSG(m_lLength < (MAX_STRING_LENGTH-10), "ASSERT: OTString::LowLevelSetStr: Exceeded MAX_STRING_LENGTH! (String would not have fully fit anyway--it would have been truncated here, potentially causing data corruption.)"); // 10 being a buffer. m_strBuffer = str_dup2(strBuf.m_strBuffer, m_lLength); } }
void String::LowLevelSetStr(const String& strBuf) { OT_ASSERT(nullptr == data_); // otherwise memory leak. if (strBuf.Exists()) { length_ = (MAX_STRING_LENGTH > strBuf.length_) ? strBuf.length_ : (MAX_STRING_LENGTH - 1); OT_ASSERT_MSG(length_ < (MAX_STRING_LENGTH - 10), "ASSERT: OTString::LowLevelSetStr: Exceeded " "MAX_STRING_LENGTH! (String would not have fully fit " "anyway--it would have been truncated here, potentially " "causing data corruption.)"); // 10 being a buffer. data_ = str_dup2(strBuf.data_, length_); } }
void OTString::LowLevelSet(const char * new_string, uint32_t nEnforcedMaxLength) { if (NULL != new_string) { if (nEnforcedMaxLength > 0) // Enforce the max length before calling strlen. If Max length is 10, then buf[9] is zero'd out. ((char *)new_string)[nEnforcedMaxLength-1] = '\0'; // Now this can never be larger than nEnforcedMaxLength // If there was already a NULL terminator, the strlen will stop there first. // Otherwise, worst case, it will be stopped by the one that I set above. uint32_t nLength = strlen(new_string); // TODO Security: use something more secure than strlen // don't bother allocating memory for a 0 length string. if (0 == nLength) return; m_lLength = nLength; m_strBuffer = str_dup2(new_string, nLength); } }
void OTString::LowLevelSet(const char * new_string, uint32_t nEnforcedMaxLength) { OT_ASSERT(NULL == m_strBuffer); // otherwise memory leak. if (NULL != new_string) { uint32_t nLength = (nEnforcedMaxLength > 0) ? strnlen(new_string, nEnforcedMaxLength) : strnlen(new_string, MAX_STRING_LENGTH-1); // don't bother allocating memory for a 0 length string. if (0 == nLength) return; m_strBuffer = str_dup2(new_string, nLength); if (NULL != m_strBuffer) m_lLength = nLength; else m_lLength = 0; } }