summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--sal/inc/rtl/string.h8
-rw-r--r--sal/inc/rtl/ustring.h8
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
/* ======================================================================= */