Kaydet (Commit) cd11bc69 authored tarafından Arnaud Versini's avatar Arnaud Versini Kaydeden (comit) Stephan Bergmann

C string usage improvment

Change-Id: I5c59f0d2d1b911ffa1ee251e0f1355d137616493
Signed-off-by: 's avatarStephan Bergmann <sbergman@redhat.com>
üst 498c3148
...@@ -135,8 +135,10 @@ static char* platformSpecific() ...@@ -135,8 +135,10 @@ static char* platformSpecific()
env = getenv( PATHVARNAME ); env = getenv( PATHVARNAME );
if (env == NULL) if (env == NULL)
return NULL; return NULL;
str = (char*) malloc( strlen( env ) + 1 );
strcpy( str, env ); str = strdup( env );
if (str == NULL)
return NULL;
/* get the tokens separated by ':' */ /* get the tokens separated by ':' */
dir = strtok( str, PATHSEPARATOR ); dir = strtok( str, PATHSEPARATOR );
...@@ -145,6 +147,12 @@ static char* platformSpecific() ...@@ -145,6 +147,12 @@ static char* platformSpecific()
{ {
/* construct soffice file path */ /* construct soffice file path */
file = (char*) malloc( strlen( dir ) + strlen( APPENDIX ) + 1 ); file = (char*) malloc( strlen( dir ) + strlen( APPENDIX ) + 1 );
if (file == NULL)
{
free(str);
return NULL;
}
strcpy( file, dir ); strcpy( file, dir );
strcat( file, APPENDIX ); strcat( file, APPENDIX );
......
...@@ -582,7 +582,7 @@ static void splash_load_image( struct splash* splash, rtl_uString* pUAppPath ) ...@@ -582,7 +582,7 @@ static void splash_load_image( struct splash* splash, rtl_uString* pUAppPath )
pLang = ustr_to_str (pLoc->Language); pLang = ustr_to_str (pLoc->Language);
pCountry = ustr_to_str (pLoc->Country); pCountry = ustr_to_str (pLoc->Country);
nLocSize = strlen (pLang->buffer) + strlen (pCountry->buffer) + 8; nLocSize = strlen (pLang->buffer) + strlen (pCountry->buffer) + 3;
pLocale = malloc (nLocSize); pLocale = malloc (nLocSize);
pLocale[0] = '-'; pLocale[0] = '-';
strcpy (pLocale + 1, pLang->buffer); strcpy (pLocale + 1, pLang->buffer);
......
This diff is collapsed.
...@@ -387,8 +387,7 @@ readoptions(char* filename, char*** pfargv) ...@@ -387,8 +387,7 @@ readoptions(char* filename, char*** pfargv)
*poptbuff=EOS; *poptbuff=EOS;
if (strlen(optbuff)>0) if (strlen(optbuff)>0)
{ {
pfa[fargc+1]=malloc(strlen(optbuff)+1); pfa[fargc+1]=strdup(optbuff);
strcpy(pfa[fargc+1],optbuff);
fargc++; fargc++;
pfa[fargc+1]=0; pfa[fargc+1]=0;
poptbuff=&optbuff[0]; poptbuff=&optbuff[0];
......
...@@ -790,12 +790,10 @@ static oslHostAddr _osl_hostentToHostAddr (const struct hostent *he) ...@@ -790,12 +790,10 @@ static oslHostAddr _osl_hostentToHostAddr (const struct hostent *he)
if (_osl_isFullQualifiedDomainName(he->h_name)) if (_osl_isFullQualifiedDomainName(he->h_name))
{ {
cn= (sal_Char *)malloc(strlen (he->h_name) + 1); cn= (sal_Char *)strdup(he->h_name);
OSL_ASSERT(cn); OSL_ASSERT(cn);
if (cn == NULL) if (cn == NULL)
return ((oslHostAddr)NULL); return ((oslHostAddr)NULL);
strcpy(cn, he->h_name);
} }
else else
{ {
...@@ -889,13 +887,11 @@ oslHostAddr SAL_CALL osl_psz_createHostAddr ( ...@@ -889,13 +887,11 @@ oslHostAddr SAL_CALL osl_psz_createHostAddr (
if ((pszHostname == NULL) || (pAddr == NULL)) if ((pszHostname == NULL) || (pAddr == NULL))
return ((oslHostAddr)NULL); return ((oslHostAddr)NULL);
cn = (sal_Char *)malloc(strlen (pszHostname) + 1); cn = (sal_Char *) strdup(pszHostname);
OSL_ASSERT(cn); OSL_ASSERT(cn);
if (cn == NULL) if (cn == NULL)
return ((oslHostAddr)NULL); return ((oslHostAddr)NULL);
strcpy (cn, pszHostname);
pHostAddr= (oslHostAddr) malloc(sizeof(struct oslHostAddrImpl)); pHostAddr= (oslHostAddr) malloc(sizeof(struct oslHostAddrImpl));
OSL_ASSERT(pHostAddr); OSL_ASSERT(pHostAddr);
if (pHostAddr == NULL) if (pHostAddr == NULL)
......
...@@ -1919,10 +1919,7 @@ namespace osl_ThreadData ...@@ -1919,10 +1919,7 @@ namespace osl_ThreadData
// at first, set the data a value // at first, set the data a value
char* pc = new char[2]; char* pc = new char[2];
char m_nData = 'm'; char m_nData = 'm';
// LLA: this is a copy functions only and really only for \0 terminated strings pc[0] = m_nData;
// m_nData is not a string, it's a character
// strcpy(pc, &m_nData);
memcpy(pc, &m_nData, 1);
pc[1] = '\0'; pc[1] = '\0';
myThreadData.setData(pc); myThreadData.setData(pc);
......
...@@ -711,10 +711,9 @@ namespace rtl_str ...@@ -711,10 +711,9 @@ namespace rtl_str
void trim_WithLength_001() void trim_WithLength_001()
{ {
char const *pStr = " trim this"; char const *pStr = " trim this";
sal_Char *pStr2 = (sal_Char*)malloc(strlen(pStr) + 1); sal_Char *pStr2 = (sal_Char*)strdup(pStr);
if (pStr2) if (pStr2)
{ {
strcpy(pStr2, pStr);
rtl_str_trim_WithLength( pStr2, 2 ); rtl_str_trim_WithLength( pStr2, 2 );
CPPUNIT_ASSERT_MESSAGE("string should be empty", strlen(pStr2) == 0); CPPUNIT_ASSERT_MESSAGE("string should be empty", strlen(pStr2) == 0);
...@@ -725,10 +724,9 @@ namespace rtl_str ...@@ -725,10 +724,9 @@ namespace rtl_str
void trim_WithLength_002() void trim_WithLength_002()
{ {
char const *pStr = "trim this"; char const *pStr = "trim this";
sal_Char *pStr2 = (sal_Char*)malloc(strlen(pStr) + 1); sal_Char *pStr2 = (sal_Char*)strdup(pStr);
if (pStr2) if (pStr2)
{ {
strcpy(pStr2, pStr);
rtl_str_trim_WithLength( pStr2, 5 ); rtl_str_trim_WithLength( pStr2, 5 );
CPPUNIT_ASSERT_MESSAGE("string should contain 'trim'", strlen(pStr2) == 4); CPPUNIT_ASSERT_MESSAGE("string should contain 'trim'", strlen(pStr2) == 4);
...@@ -739,7 +737,7 @@ namespace rtl_str ...@@ -739,7 +737,7 @@ namespace rtl_str
void trim_WithLength_003() void trim_WithLength_003()
{ {
char const *pStr = " trim this"; char const *pStr = " trim this";
sal_Char *pStr2 = (sal_Char*)malloc(strlen(pStr) + 1); sal_Char *pStr2 = (sal_Char*)strdup(pStr);
if (pStr2) if (pStr2)
{ {
strcpy(pStr2, pStr); strcpy(pStr2, pStr);
...@@ -753,10 +751,9 @@ namespace rtl_str ...@@ -753,10 +751,9 @@ namespace rtl_str
void trim_WithLength_004() void trim_WithLength_004()
{ {
char const *pStr = "\r\n\t \n\r trim \n this"; char const *pStr = "\r\n\t \n\r trim \n this";
sal_Char *pStr2 = (sal_Char*)malloc(strlen(pStr) + 1); sal_Char *pStr2 = (sal_Char*)strdup(pStr);
if (pStr2) if (pStr2)
{ {
strcpy(pStr2, pStr);
rtl_str_trim_WithLength( pStr2, 17 ); rtl_str_trim_WithLength( pStr2, 17 );
CPPUNIT_ASSERT_MESSAGE("string should contain 'trim'", strlen(pStr2) == 4); CPPUNIT_ASSERT_MESSAGE("string should contain 'trim'", strlen(pStr2) == 4);
...@@ -767,10 +764,9 @@ namespace rtl_str ...@@ -767,10 +764,9 @@ namespace rtl_str
void trim_WithLength_005() void trim_WithLength_005()
{ {
char const *pStr = "\r\n\t \n\r trim \t this \n\r\t\t "; char const *pStr = "\r\n\t \n\r trim \t this \n\r\t\t ";
sal_Char *pStr2 = (sal_Char*)malloc(strlen(pStr) + 1); sal_Char *pStr2 = (sal_Char*)strdup(pStr);
if (pStr2) if (pStr2)
{ {
strcpy(pStr2, pStr);
rtl_str_trim_WithLength( pStr2, strlen(pStr2) ); rtl_str_trim_WithLength( pStr2, strlen(pStr2) );
CPPUNIT_ASSERT_MESSAGE("string should contain 'trim'", strlen(pStr2) == 11); CPPUNIT_ASSERT_MESSAGE("string should contain 'trim'", strlen(pStr2) == 11);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment