Avoid calling OString ctor with null pointer

...in preparation of potential future changes from using OString to using
std::string_view, where OString has an undocumented feature of allowing
construction from a null pointer.

This is mostly the result of a manual audit of potentially problematic getenv
calls across the code base.  But there can be other problematic places too, like
the xmlGetProp call in tools/source/xml/XmlWalker.cxx.  To identify those,
rtl_{string,uString}_newFromStr aborts now in non-production debug builds when a
null pointer is passed(and all places that hit with a full `make check
screenshot` have been addressed here).  Once we are confident that all
problematic places have been identified, we should drop support for the
undocumented feature (see the TODO in sal/rtl/strtmpl.cxx).

Change-Id: I595cc6d4f1cda74add2a3db171323f817d362b08
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/107430
Tested-by: Jenkins
Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
This commit is contained in:
Stephan Bergmann
2020-12-10 16:27:35 +01:00
parent 65b1152f5c
commit 4f0c70fb55
9 changed files with 34 additions and 9 deletions
+2 -1
View File
@@ -277,7 +277,8 @@ SvCommand::SvCommand( int argc, char ** argv )
aList.clear();
OString aInc(getenv("INCLUDE"));
auto const env = getenv("INCLUDE");
OString aInc(env == nullptr ? "" : env);
// append include environment variable
if( aInc.getLength() )
{
+5 -1
View File
@@ -512,7 +512,11 @@ oslHostAddr SAL_CALL osl_createHostAddrByName(rtl_uString *strHostname)
{
pRet = static_cast<oslHostAddr>(
rtl_allocateZeroMemory(sizeof(struct oslHostAddrImpl)));
rtl_uString_newFromStr(&pRet->pHostName, o3tl::toU(pIter->ai_canonname));
if (pIter->ai_canonname == nullptr) {
rtl_uString_new(&pRet->pHostName);
} else {
rtl_uString_newFromStr(&pRet->pHostName, o3tl::toU(pIter->ai_canonname));
}
pRet->pSockAddr = createSocketAddr();
memcpy(& pRet->pSockAddr->m_sockaddr,
pIter->ai_addr, pIter->ai_addrlen);
+2 -2
View File
@@ -931,7 +931,7 @@ namespace osl_FileBase
OString sURL("file://~/tmp");
char* home_path;
home_path = getenv("HOME");
OString expResult(home_path);
OString expResult(home_path ? home_path : "");
expResult += "/tmp";
checkUNXBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_None, expResult);
#endif
@@ -1009,7 +1009,7 @@ namespace osl_FileBase
OString sSysPath("~/tmp");
char* home_path;
home_path = getenv("HOME");
OString expResult(home_path);
OString expResult(home_path ? home_path : "");
expResult = "file://"+ expResult + "/tmp";
checkUNXBehaviour_getFileURLFromSystemPath(sSysPath, osl::FileBase::E_None, expResult);
checkWNTBehaviour_getFileURLFromSystemPath(sSysPath, osl::FileBase::E_None, "~/tmp");
+10
View File
@@ -24,6 +24,7 @@
#include <algorithm>
#include <cassert>
#include <cstdlib>
#include <limits>
#include <cstring>
@@ -1324,6 +1325,15 @@ void SAL_CALL IMPL_RTL_STRINGNAME( newFromStr )( IMPL_RTL_STRINGDATA** ppThis,
IMPL_RTL_STRINGDATA* pOrg;
sal_Int32 nLen;
#if OSL_DEBUG_LEVEL > 0
//TODO: For now, only abort in non-production debug builds; once all places that rely on the
// undocumented newFromStr behavior of treating a null pCharStr like an empty string have been
// found and fixed, drop support for that behavior and turn this into a general assert:
if (pCharStr == nullptr) {
std::abort();
}
#endif
if ( pCharStr )
{
nLen = IMPL_RTL_STRNAME( getLength )( pCharStr );
+3 -1
View File
@@ -47,7 +47,9 @@ ScreenshotTest::ScreenshotTest()
, maParent(nullptr, "vcl/ui/screenshotparent.ui", "ScreenShot")
, mxParentWidget(maParent.getDialog()->weld_content_area())
{
maCurrentLanguage = OUString::fromUtf8(getenv("LO_TEST_LOCALE"));
if (auto const env = getenv("LO_TEST_LOCALE")) {
maCurrentLanguage = OUString::fromUtf8(env);
}
}
ScreenshotTest::~ScreenshotTest()
+2 -1
View File
@@ -90,7 +90,8 @@ OString XmlWalker::attribute(const OString& sName)
{
xmlChar* xmlName = xmlCharStrdup(sName.getStr());
xmlChar* xmlAttribute = xmlGetProp(mpImpl->mpCurrent, xmlName);
OString aAttributeContent(reinterpret_cast<const char*>(xmlAttribute));
OString aAttributeContent(
xmlAttribute == nullptr ? "" : reinterpret_cast<const char*>(xmlAttribute));
xmlFree(xmlAttribute);
xmlFree(xmlName);
+3 -1
View File
@@ -50,7 +50,9 @@ static OUString getPdfDir( const PrinterInfo& rInfo )
sal_Int32 nPos = 0;
aDir = aToken.getToken( 1, '=', nPos );
if( aDir.isEmpty() )
aDir = OStringToOUString( OString( getenv( "HOME" ) ), osl_getThreadTextEncoding() );
if (auto const env = getenv( "HOME" )) {
aDir = OStringToOUString( OString( env ), osl_getThreadTextEncoding() );
}
break;
}
}
+4 -1
View File
@@ -50,7 +50,10 @@ static OUString getPdfDir(const PrinterInfo& rInfo)
sal_Int32 nPos = 0;
aDir = aToken.getToken(1, '=', nPos);
if (aDir.isEmpty())
aDir = OStringToOUString(OString(getenv("HOME")), osl_getThreadTextEncoding());
if (auto const env = getenv("HOME"))
{
aDir = OStringToOUString(OString(env), osl_getThreadTextEncoding());
}
break;
}
}
+3 -1
View File
@@ -675,7 +675,9 @@ SelectionManager& SelectionManager::get( const OUString& rDisplayName )
OUString aDisplayName( rDisplayName );
if( aDisplayName.isEmpty() )
aDisplayName = OStringToOUString( getenv( "DISPLAY" ), RTL_TEXTENCODING_ISO_8859_1 );
if (auto const env = getenv( "DISPLAY" )) {
aDisplayName = OStringToOUString( env, RTL_TEXTENCODING_ISO_8859_1 );
}
SelectionManager* pInstance = nullptr;
std::unordered_map< OUString, SelectionManager* >::iterator it = getInstances().find( aDisplayName );