Skip to content

Don't panic when user provides null pointers to methods #253

@muzarski

Description

@muzarski

Currently, we assume that user uses the API correctly, and we panic if the provided pointer is null.

Example:

#[unsafe(no_mangle)]
pub unsafe extern "C" fn cass_cluster_set_connect_timeout(
    cluster_raw: CassBorrowedExclusivePtr<CassCluster, CMut>,
    timeout_ms: c_uint,
) {
    let cluster = BoxFFI::as_mut_ref(cluster_raw).unwrap();
    cluster.session_builder.config.connect_timeout = Duration::from_millis(timeout_ms.into());
}

This unwrap() panics if cluster_raw is null.

This should be converted to something like:

#[unsafe(no_mangle)]
pub unsafe extern "C" fn cass_cluster_set_connect_timeout(
    cluster_raw: CassBorrowedExclusivePtr<CassCluster, CMut>,
    timeout_ms: c_uint,
) {
    let Some(cluster) = BoxFFI::as_mut_ref(cluster_raw) else {
        tracing::error!("Provided null pointer to cass_cluster_set_connect_timeout!");
        return CassError::CASS_ERROR_LIB_BAD_PARAMS;
    };
    cluster.session_builder.config.connect_timeout = Duration::from_millis(timeout_ms.into());
}

Metadata

Metadata

Assignees

Labels

P1P1 priority item - very importantgood first issueGood for newcomers

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions