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.
This commit is contained in:
Martin Whitaker 2025-10-18 22:31:07 +01:00
parent 884349caab
commit 5f651d944b
1 changed files with 7 additions and 1 deletions

View File

@ -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);