On Fri, Dec 01, 2017 at 03:16:23PM -0500, Jason Merrill wrote:
> commit e39b7d506d236ce7ef9f64d1bcf0b384bb2d8038
> Author: Jason Merrill <ja...@redhat.com>
> Date:   Fri Dec 1 07:45:03 2017 -0500
> 
>             PR c++/79228 - extensions hide C++14 complex literal operators
>     
>     libcpp/
>             * expr.c (interpret_float_suffix): Ignore 'i' in C++14 and up.
>             (interpret_int_suffix): Likewise.
>     gcc/cp/
>             * parser.c (cp_parser_userdef_numeric_literal): Be helpful about
>             'i' in C++14 and up.
> 
> +      /* In C++14 and up these suffixes are in the standard library, so treat
> +      them as user-defined literals.  */
> +      if (CPP_OPTION (pfile, cplusplus)
> +       && CPP_OPTION (pfile, lang) > CLK_CXX11
> +       && (!memcmp (orig_s, "i", orig_len)
> +           || !memcmp (orig_s, "if", orig_len)
> +           || !memcmp (orig_s, "il", orig_len)))

This doesn't seem right, it will invoke UB if orig_len > 2 by potentially
accessing bytes beyond 'i' and '\0' in "i" (and for orig_len > 3 also after
"if" or "il").
If you only want to return 0 if orig_len bytes starting at orig_s are 'i'
or 'i' 'f' or 'i' 'l', then I'd write instead as in the patch below.
Or if memcmp is more readable, at least check orig_len first.

> +      /* In C++14 and up these suffixes are in the standard library, so treat
> +      them as user-defined literals.  */
> +      if (CPP_OPTION (pfile, cplusplus)
> +       && CPP_OPTION (pfile, lang) > CLK_CXX11
> +       && (!memcmp (s, "i", orig_len)
> +           || !memcmp (s, "if", orig_len)
> +           || !memcmp (s, "il", orig_len)))

Similarly.  Additionally, "if" can't happen here, because we don't allow 'f'
among suffixes.

So, ok for trunk if it passes testing, or some other form thereof?

2017-12-05  Jakub Jelinek  <ja...@redhat.com>

        PR c++/79228
        * expr.c (interpret_float_suffix): Avoid memcmp.
        (interpret_int_suffix): Likewise.  Don't check for if.

--- libcpp/expr.c.jj    2017-12-01 22:13:24.000000000 +0100
+++ libcpp/expr.c       2017-12-05 13:26:57.019683785 +0100
@@ -280,9 +280,10 @@ interpret_float_suffix (cpp_reader *pfil
         them as user-defined literals.  */
       if (CPP_OPTION (pfile, cplusplus)
          && CPP_OPTION (pfile, lang) > CLK_CXX11
-         && (!memcmp (orig_s, "i", orig_len)
-             || !memcmp (orig_s, "if", orig_len)
-             || !memcmp (orig_s, "il", orig_len)))
+         && orig_s[0] == 'i'
+         && (orig_len == 1
+             || (orig_len == 2
+                 && (orig_s[1] == 'f' || orig_s[1] == 'l'))))
        return 0;
     }
 
@@ -345,9 +346,8 @@ interpret_int_suffix (cpp_reader *pfile,
         them as user-defined literals.  */
       if (CPP_OPTION (pfile, cplusplus)
          && CPP_OPTION (pfile, lang) > CLK_CXX11
-         && (!memcmp (s, "i", orig_len)
-             || !memcmp (s, "if", orig_len)
-             || !memcmp (s, "il", orig_len)))
+         && s[0] == 'i'
+         && (orig_len == 1 || (orig_len == 2 && s[1] == 'l')))
        return 0;
     }
 


        Jakub

Reply via email to