* [PATCH 3/4] parser: accept typedef names as offsetof members
@ 2026-11-09 20:09 Kris Van Hees
0 siblings, 0 replies; only message in thread
From: Kris Van Hees @ 2026-11-09 20:09 UTC (permalink / raw)
To: dtrace, dtrace-devel
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.
Signed-off-by: Kris Van Hees <kris.van.hees@oracle.com>
---
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
diff --git a/libdtrace/dt_grammar.y b/libdtrace/dt_grammar.y
index 7984b85d..83feb9f7 100644
--- a/libdtrace/dt_grammar.y
+++ b/libdtrace/dt_grammar.y
@@ -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); }
diff --git a/test/unittest/offsetof/tst.typedef-member.d b/test/unittest/offsetof/tst.typedef-member.d
new file mode 100644
index 00000000..e3838b61
--- /dev/null
+++ b/test/unittest/offsetof/tst.typedef-member.d
@@ -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);
+}
diff --git a/test/unittest/offsetof/tst.typedef-member.r b/test/unittest/offsetof/tst.typedef-member.r
new file mode 100644
index 00000000..96366d41
--- /dev/null
+++ b/test/unittest/offsetof/tst.typedef-member.r
@@ -0,0 +1,2 @@
+offset is 1
+
--
2.52.0
^ permalink raw reply related [flat|nested] only message in thread
only message in thread, other threads:[~2026-11-09 20:11 UTC | newest]
Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-09 20:09 [PATCH 3/4] parser: accept typedef names as offsetof members Kris Van Hees
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox