diff options
author | Luboš Luňák <l.lunak@suse.cz> | 2012-03-08 00:16:42 +0100 |
---|---|---|
committer | Luboš Luňák <l.lunak@suse.cz> | 2012-03-12 13:35:56 +0100 |
commit | 715bdfdd8f994607f07bb577c727189a3a434607 (patch) | |
tree | 6009392b03749b5c49163720084acca60f3b0ea3 /sal/inc/rtl | |
parent | a9435ab4d784879a299028e2ca5346510d5e616d (diff) |
prevent using RTL_CONSTASCII_* macros with string literal functions
This is to prevent things like by mistake doing match( RTL_CONSTASCII_STRINGPARAM("foo")),
which will call match(const char(&)[N], int=0), where the second argument is the fromIndex
argument.
Diffstat (limited to 'sal/inc/rtl')
-rw-r--r-- | sal/inc/rtl/string.h | 8 | ||||
-rw-r--r-- | sal/inc/rtl/ustring.h | 8 |
2 files changed, 14 insertions, 2 deletions
diff --git a/sal/inc/rtl/string.h b/sal/inc/rtl/string.h index d9e9489e6069..f695c1207021 100644 --- a/sal/inc/rtl/string.h +++ b/sal/inc/rtl/string.h @@ -1194,7 +1194,13 @@ SAL_DLLPUBLIC sal_Int32 SAL_CALL rtl_string_getToken( its value should be 0x00. Depending on where this macro is used, the nature of the supplied expression might be further restricted. */ -#define RTL_CONSTASCII_STRINGPARAM( constAsciiStr ) constAsciiStr, ((sal_Int32)SAL_N_ELEMENTS(constAsciiStr)-1) +// The &foo[0] trick is intentional, it makes sure the type is char* or const char* +// (plain cast to const char* would not work with non-const char foo[]="a", which seems to be allowed). +// This is to avoid mistaken use with functions that accept string literals +// (i.e. const char (&)[N]) where usage of this macro otherwise could match +// the argument and a following int argument with a default value (e.g. OString::match()). +#define RTL_CONSTASCII_STRINGPARAM( constAsciiStr ) (&(constAsciiStr)[0]), \ + ((sal_Int32)SAL_N_ELEMENTS(constAsciiStr)-1) /** Supply the length of an ASCII string literal. diff --git a/sal/inc/rtl/ustring.h b/sal/inc/rtl/ustring.h index e432d2c831fe..5cf2b4bcf043 100644 --- a/sal/inc/rtl/ustring.h +++ b/sal/inc/rtl/ustring.h @@ -1682,7 +1682,13 @@ SAL_DLLPUBLIC sal_Int32 SAL_CALL rtl_uString_getToken( its value should be 0x00. Depending on where this macro is used, the nature of the supplied expression might be further restricted. */ -#define RTL_CONSTASCII_USTRINGPARAM( constAsciiStr ) constAsciiStr, ((sal_Int32)(SAL_N_ELEMENTS(constAsciiStr)-1)), RTL_TEXTENCODING_ASCII_US +// The &foo[0] trick is intentional, it makes sure the type is char* or const char* +// (plain cast to const char* would not work with non-const char foo[]="a", which seems to be allowed). +// This is to avoid mistaken use with functions that accept string literals +// (i.e. const char (&)[N]) where usage of this macro otherwise could match +// the argument and a following int argument with a default value (e.g. OUString::match()). +#define RTL_CONSTASCII_USTRINGPARAM( constAsciiStr ) (&(constAsciiStr)[0]), \ + ((sal_Int32)(SAL_N_ELEMENTS(constAsciiStr)-1)), RTL_TEXTENCODING_ASCII_US /* ======================================================================= */ |