Message ID | 20130806144207.GB17022@redhat.com |
---|---|
State | New |
Headers | show |
On 08/06/2013 10:42 AM, Marek Polacek wrote: > Hm, actually, we can't easily fold the call to the sanitize function > away, I'm afraid, if we want to do it for the 'case <something>' > case. When we hit the DIV_EXPR in 'case 0 * (1 / 0)', > the ubsan_instrument_division gets 1 as a first argument and 0 as > a second argument, but due to fold_builds in the > ubsan_instrument_division, we replace the case value with just the call > to the __builtin___ubsan_handle_divrem_overflow. Ah, and the call isn't folded away because it has side-effects. > I think, what we could do, is to tweak verify_constant like this: > > + /* This is to handle e.g. the goofy 'case 0 * (1 / 0)' case. */ > + if (flag_sanitize & SANITIZE_UNDEFINED > + && TREE_CODE (t) == CALL_EXPR > + && is_ubsan_builtin (t)) > + { > + error ("undefined behavior occured"); > + return *non_constant_p; > + } I think I'd rather handle ubsan builtins specially in dump_expr. Jason
On Tue, Aug 06, 2013 at 07:07:27PM -0400, Jason Merrill wrote: > >I think, what we could do, is to tweak verify_constant like this: > > > >+ /* This is to handle e.g. the goofy 'case 0 * (1 / 0)' case. */ > >+ if (flag_sanitize & SANITIZE_UNDEFINED > >+ && TREE_CODE (t) == CALL_EXPR > >+ && is_ubsan_builtin (t)) > >+ { > >+ error ("undefined behavior occured"); > >+ return *non_constant_p; > >+ } > > I think I'd rather handle ubsan builtins specially in dump_expr. I might've misunderstood what you mean. If we drop the hunk above, then we'll call error ("%q+E is not a constant expression", t); so, we'll print "is not a constant expression" no matter what, we surely can recognize the ubsan built-ins in dump_expr, but what would we do then? Marek
On 08/07/2013 06:06 AM, Marek Polacek wrote: > I might've misunderstood what you mean. If we drop the hunk above, > then we'll call > error ("%q+E is not a constant expression", t); > so, we'll print "is not a constant expression" no matter what Yes, that's fine; 1/0 is not a constant expression, because it has undefined behavior. > we surely can recognize the ubsan built-ins in dump_expr, but what would > we do then? Print something more meaningful to the user. Jason
--- a/gcc/cp/semantics.c +++ b/gcc/cp/semantics.c @@ -6938,6 +6938,14 @@ static bool verify_constant (tree t, bool allow_non_constant, bool *non_constant_p, bool *overflow_p) { + /* This is to handle e.g. the goofy 'case 0 * (1 / 0)' case. */ + if (flag_sanitize & SANITIZE_UNDEFINED + && TREE_CODE (t) == CALL_EXPR + && is_ubsan_builtin (t)) + { + error ("undefined behavior occured"); + return *non_constant_p; + } if (!*non_constant_p && !reduced_constant_expression_p (t)) { if (!allow_non_constant)