Skip to content

Conditionalize null ptr check when casting #35100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions stdlib/public/runtime/DynamicCast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ extern "C" const StructDescriptor NOMINAL_TYPE_DESCR_SYM(Sh);
/// Nominal type descriptor for Swift.String.
extern "C" const StructDescriptor NOMINAL_TYPE_DESCR_SYM(SS);

// If this returns `true`, then we will call `fatalError` when we encounter a
// null reference in a storage locaation whose type does not allow null.
static bool unexpectedNullIsFatal() {
return true; // Placeholder for an upcoming check.
}

static HeapObject * getNonNullSrcObject(OpaqueValue *srcValue,
const Metadata *srcType,
const Metadata *destType) {
Expand All @@ -120,12 +126,21 @@ static HeapObject * getNonNullSrcObject(OpaqueValue *srcValue,

std::string srcTypeName = nameForMetadata(srcType);
std::string destTypeName = nameForMetadata(destType);
swift::fatalError(/* flags = */ 0,
"Found unexpected null pointer value"
const char *msg = "Found unexpected null pointer value"
" while trying to cast value of type '%s' (%p)"
" to '%s' (%p)\n",
srcTypeName.c_str(), srcType,
destTypeName.c_str(), destType);
" to '%s' (%p)%s\n";
if (unexpectedNullIsFatal()) {
swift::fatalError(/* flags = */ 0, msg,
srcTypeName.c_str(), srcType,
destTypeName.c_str(), destType,
"");
} else {
swift::warning(/* flags = */ 0, msg,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might end up firing too often, if this ends up getting hit in a loop or something. I don't think you need to do this yet, but in the future this may need a flag or count to limit how many times the warning is logged in a given process.

srcTypeName.c_str(), srcType,
destTypeName.c_str(), destType,
": Continuing with null object, but expect problems later.");
}
return object;
}

/******************************************************************************/
Expand Down