From b223a78a71b9f000315d70987d600661420f9475 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=9C=E9=83=A8=E6=98=8C=E5=B9=B3?= Date: Fri, 31 Jan 2020 15:25:09 +0900 Subject: [PATCH] this ternary operator is an undefined behaviour MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Let me quote ISO/IEC 9899:2018 section 6.5.15: > Constraints > > The first operand shall have scalar type. > One of the following shall hold for the second and third operands: > — both operands have arithmetic type; > — both operands have the same structure or union type; > — both operands have void type; (snip) Here, `*option` is a const struct rb_compile_option_struct. OTOH `COMPILE_OPTION_DEFAULT` is a struct rb_compile_option_struct, without const. These two are _not_ the "same structure or union type". Hence the expression renders undefined behaviour. COMPILE_OPTION_DEFAULT is not a const because `RubyVM::InstructionSequence.compile_option=` touches its internals on-the-fly. There is no way to meet the constraints quoted above. Using ternary operator here was a mistake at the first place. Let's just replace it with a normal `if` statement. --- iseq.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/iseq.c b/iseq.c index a294f08527..b06f23c33d 100644 --- a/iseq.c +++ b/iseq.c @@ -825,7 +825,12 @@ rb_iseq_new_with_opt(const rb_ast_body_t *ast, VALUE name, VALUE path, VALUE rea rb_iseq_t *iseq = iseq_alloc(); rb_compile_option_t new_opt; - new_opt = option ? *option : COMPILE_OPTION_DEFAULT; + if (option) { + new_opt = *option; + } + else { + new_opt = COMPILE_OPTION_DEFAULT; + } if (ast && ast->compile_option) rb_iseq_make_compile_option(&new_opt, ast->compile_option); prepare_iseq_build(iseq, name, path, realpath, first_lineno, node ? &node->nd_loc : NULL, node ? nd_node_id(node) : -1, parent, type, &new_opt);