From 5f651d944bf2d4e286519237cff52e69d8d43414 Mon Sep 17 00:00:00 2001 From: Martin Whitaker Date: Sat, 18 Oct 2025 22:31:07 +0100 Subject: [PATCH] Check that pointers returned by tf_getp are valid. When the argument is a literal string, tf_getp returns a pointer to the string. But the return type is a PLI_INT32, so on machines where pointers are larger than 32 bits, the pointer value may get truncated. Check for this at run time, and if it occurs, print a warning and return 0. --- libveriuser/getp.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/libveriuser/getp.c b/libveriuser/getp.c index e315eead6..6122080e2 100644 --- a/libveriuser/getp.c +++ b/libveriuser/getp.c @@ -53,7 +53,13 @@ PLI_INT32 tf_igetp(PLI_INT32 n, void *obj) vpi_get_value(arg_h, &value); /* The following may generate a compilation warning, but this * functionality is required by some versions of the standard. */ - rtn = (int)(intptr_t)value.value.str; /* Oh my */ + rtn = (PLI_INT32)(intptr_t)value.value.str; /* Oh my */ + if ((intptr_t)rtn != (intptr_t)value.value.str) { + fprintf(stderr, "warning: tf_getp returning string pointer. " + "Pointer value doesn't fit in 32 bits, " + "returning 0.\n"); + rtn = 0; + } } else { value.format = vpiIntVal; vpi_get_value(arg_h, &value);