Skip to contentSkip to conversation

parser: accept typedef names as offsetof members

PATCH New Kris Van Hees 1 participant 1 message Nov 9

lore
Share & tools
lore
#0 lore raw

PATCH 3/4 parser: accept typedef names as offsetof members

Allow the member argument of offsetof() to be either an identifier or a type-name token. Kernel typedefs such as code can otherwise cause valid member names to being recognized during parsing.

---
 libdtrace/dt_grammar.y                      | 12 ++++++++++--
 test/unittest/offsetof/tst.typedef-member.d | 21 +++++++++++++++++++++
 test/unittest/offsetof/tst.typedef-member.r |  2 ++
 3 files changed, 33 insertions(+), 2 deletions(-)
 create mode 100644 test/unittest/offsetof/tst.typedef-member.d
 create mode 100644 test/unittest/offsetof/tst.typedef-member.r
libdtrace/dt_grammar.y +10 -2
index 7984b85d..83feb9f7 100644
@@ -1,7 +1,7 @@
 %{
 /*
  * Oracle Linux DTrace.
- * Copyright (c) 2006, 2025, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2006, 2026, Oracle and/or its affiliates. All rights reserved.
  * Licensed under the Universal Permissive License v 1.0 as shown at
  * http://oss.oracle.com/licenses/upl.
  */
@@ -153,6 +153,8 @@ int yylex (void);
 %type	<l_decl>	direct_abstract_declarator
 %type	<l_decl>	translator_input_type
 
+%type	<l_str>		offsetof_member
+
 %type	<l_node>	parameter_type_list
 %type	<l_node>	parameter_list
 %type	<l_node>	parameter_declaration
@@ -387,7 +389,7 @@ postfix_expression:
 			$$ = OP1(DT_TOK_POSTDEC, $1);
 		}
 	|	DT_TOK_OFFSETOF DT_TOK_LPAR type_name DT_TOK_COMMA 
-		    DT_TOK_IDENT DT_TOK_RPAR {
+		    offsetof_member DT_TOK_RPAR {
 			$$ = dt_node_offsetof($3, $5);
 		}
 	|	DT_TOK_XLATE DT_TOK_LT type_name DT_TOK_GT
@@ -396,6 +398,12 @@ postfix_expression:
 		}
 	;
 
+/* A member name can also be a typedef name in an unrelated scope. */
+offsetof_member:
+		DT_TOK_IDENT
+	|	DT_TOK_TNAME
+	;
+
 unary_expression:
 		postfix_expression
 	|	DT_TOK_ADDADD unary_expression { $$ = OP1(DT_TOK_PREINC, $2); }
test/unittest/offsetof/tst.typedef-member.d new file +21 -0
new file mode 100644
index 00000000..e3838b61
@@ -0,0 +1,21 @@
+/*
+ * Oracle Linux DTrace.
+ * Copyright (c) 2026, Oracle and/or its affiliates. All rights reserved.
+ * Licensed under the Universal Permissive License v 1.0 as shown at
+ * http://oss.oracle.com/licenses/upl.
+ */
+
+/*
+ * ASSERTION:
+ * offsetof() accepts a member name that is also a typedef name.  The kernel
+ * defines both the code typedef and flow_dissector_key_icmp.code, which is
+ * inside an anonymous struct.
+ */
+
+#pragma D option quiet
+
+dtrace:::BEGIN
+{
+	printf("offset is %d\n", offsetof(struct `flow_dissector_key_icmp, code));
+	exit(offsetof(struct `flow_dissector_key_icmp, code) != 1);
+}
test/unittest/offsetof/tst.typedef-member.r new file +2 -0
new file mode 100644
index 00000000..96366d41
@@ -0,0 +1,2 @@
+offset is 1
+
Signature · 1 line
2.52.0