This is an automated email from the ASF dual-hosted git repository.
Xuanwo pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/opendal.git
The following commit(s) were added to refs/heads/main by this push:
new 8f30703639 docs(bindings/c): state the borrowed string rule for write
options (#9409)
8f30703639 is described below
commit 8f30703639ce0cadc760f3be0cd7a8066766d026
Author: Chitral Verma <[email protected]>
AuthorDate: Mon Nov 30 06:28:28 2026 +0530
docs(bindings/c): state the borrowed string rule for write options (#9409)
* docs(bindings/c): state the borrowed string rule for write options
State in the # Safety docs of opendal_operator_write_with,
opendal_operator_write_with_metadata, opendal_operator_write_with_cancel,
opendal_operator_writer_with, opendal_operator_writer_with_cancel,
opendal_operator_presign_write_with, and
opendal_operator_presign_write_with_cancel that each non-null string in
opts and each non-null key and value in its user metadata pairs must be a
readable, nul-terminated C string until the call returns, and that a
string that is not valid UTF-8 panics, as the read, stat, delete, list,
and copy entry points state.
State on the writer entry points that the returned writer does not read
the options.
No API, ABI, or behavior change.
Signed-off-by: Chitral Verma <[email protected]>
* fixup! docs(bindings/c): state the borrowed string rule for write options
Say in the user_metadata member, setter, and pair docs that the pairs and
their strings must stay valid until the function that takes the options
returns, as the string member docs say, instead of "until the operation
that uses the options returns". A writer caller could read "operation" as
the writer's whole lifetime, which contradicts the new writer_with rule.
Signed-off-by: Chitral Verma <[email protected]>
---------
Signed-off-by: Chitral Verma <[email protected]>
---
bindings/c/include/opendal.h | 83 +++++++++++++++++++++++++++++++++++++++----
bindings/c/src/operator.rs | 51 ++++++++++++++++++++++++++
bindings/c/src/options.rs | 8 ++---
bindings/c/src/presign.rs | 20 +++++++++++
bindings/c/src/types.rs | 4 +--
dev/src/generate/c/options.rs | 11 +++---
6 files changed, 160 insertions(+), 17 deletions(-)
diff --git a/bindings/c/include/opendal.h b/bindings/c/include/opendal.h
index baf9643eaf..c6e44fefe8 100644
--- a/bindings/c/include/opendal.h
+++ b/bindings/c/include/opendal.h
@@ -344,13 +344,13 @@ typedef struct opendal_operator_options {
typedef struct opendal_write_user_metadata_pair {
/**
* The metadata key. A non-null key must point to a nul-terminated UTF-8
- * string that stays valid until the operation that uses the options
+ * string that stays valid until the function that takes the options
* returns.
*/
const char *key;
/**
* The metadata value. A non-null value must point to a nul-terminated
- * UTF-8 string that stays valid until the operation that uses the options
+ * UTF-8 string that stays valid until the function that takes the options
* returns.
*/
const char *value;
@@ -684,7 +684,7 @@ typedef struct opendal_write_options {
* NULL or a `user_metadata_len` of `0` means unset.
* `opendal_write_options_set_user_metadata()` stores this pointer without
copying
* the pairs or their strings, and `opendal_write_options_free()` does not
free
- * them, so they must stay valid until the operation that uses these options
+ * them, so they must stay valid until the function that takes these options
* returns. A pair whose `key` or `value` is NULL is skipped, and a later
pair
* replaces an earlier pair with the same key.
*/
@@ -2550,6 +2550,16 @@ struct opendal_error *opendal_operator_write(const
struct opendal_operator *op,
/**
* \brief Blocking write raw bytes to `path` with options.
+ *
+ * # Safety
+ *
+ * * If `opts` is not NULL, each non-NULL string member of `*opts` must point
to a
+ * readable, nul-terminated C string until this function returns. A non-NULL
+ * `user_metadata` must point to `user_metadata_len` readable pairs, and
each non-NULL
+ * key and value in them to a readable, nul-terminated C string, until this
function
+ * returns. A string that is not valid UTF-8 makes this function panic. The
+ * `opendal_write_options` string and user metadata setters store the
caller's
+ * pointers, not copies.
*/
struct opendal_error *opendal_operator_write_with(const struct
opendal_operator *op,
const char *path,
@@ -2582,6 +2592,13 @@ struct opendal_error *opendal_operator_write_with(const
struct opendal_operator
* * The `bytes` provided has valid byte in the `data` field and the `len`
field is set
* correctly.
* * `meta` must be a valid, aligned, writable pointer.
+ * * If `opts` is not NULL, each non-NULL string member of `*opts` must point
to a
+ * readable, nul-terminated C string until this function returns. A non-NULL
+ * `user_metadata` must point to `user_metadata_len` readable pairs, and
each non-NULL
+ * key and value in them to a readable, nul-terminated C string, until this
function
+ * returns. A string that is not valid UTF-8 makes this function panic. The
+ * `opendal_write_options` string and user metadata setters store the
caller's
+ * pointers, not copies.
*
* # Panic
*
@@ -2608,6 +2625,16 @@ struct opendal_error
*opendal_operator_write_with_metadata(const struct opendal_
* @param cancel Optional cancel token, or NULL
* @param meta Optional output pointer receiving the allocated
`opendal_metadata *`, or NULL
* @return NULL if the operation succeeds, or an `opendal_error *` on
cancellation or failure.
+ *
+ * # Safety
+ *
+ * * If `opts` is not NULL, each non-NULL string member of `*opts` must point
to a
+ * readable, nul-terminated C string until this function returns. A non-NULL
+ * `user_metadata` must point to `user_metadata_len` readable pairs, and
each non-NULL
+ * key and value in them to a readable, nul-terminated C string, until this
function
+ * returns. A string that is not valid UTF-8 makes this function panic. The
+ * `opendal_write_options` string and user metadata setters store the
caller's
+ * pointers, not copies.
*/
struct opendal_error *opendal_operator_write_with_cancel(const struct
opendal_operator *op,
const char *path,
@@ -2857,6 +2884,18 @@ struct opendal_error *opendal_operator_writer(const
struct opendal_operator *op,
/**
* \brief Blocking create a writer for the specified path with options.
+ *
+ * # Safety
+ *
+ * * If `opts` is not NULL, each non-NULL string member of `*opts` must point
to a
+ * readable, nul-terminated C string until this function returns. A non-NULL
+ * `user_metadata` must point to `user_metadata_len` readable pairs, and
each non-NULL
+ * key and value in them to a readable, nul-terminated C string, until this
function
+ * returns. A string that is not valid UTF-8 makes this function panic. The
+ * `opendal_write_options` string and user metadata setters store the
caller's
+ * pointers, not copies.
+ * The returned writer does not read `*opts`, so the caller can free the
options,
+ * their strings, and the pairs when this function returns.
*/
struct opendal_error *opendal_operator_writer_with(const struct
opendal_operator *op,
const char *path,
@@ -2869,6 +2908,18 @@ struct opendal_error *opendal_operator_writer_with(const
struct opendal_operator
* Pass NULL for `opts` to use defaults, and NULL for `cancel` to run without
cancellation.
* On cancellation or failure, writes NULL to `*writer` and returns an
`opendal_error *`
* (`OPENDAL_CANCELLED` on cancellation).
+ *
+ * # Safety
+ *
+ * * If `opts` is not NULL, each non-NULL string member of `*opts` must point
to a
+ * readable, nul-terminated C string until this function returns. A non-NULL
+ * `user_metadata` must point to `user_metadata_len` readable pairs, and
each non-NULL
+ * key and value in them to a readable, nul-terminated C string, until this
function
+ * returns. A string that is not valid UTF-8 makes this function panic. The
+ * `opendal_write_options` string and user metadata setters store the
caller's
+ * pointers, not copies.
+ * The returned writer does not read `*opts`, so the caller can free the
options,
+ * their strings, and the pairs when this function returns.
*/
struct opendal_error *opendal_operator_writer_with_cancel(const struct
opendal_operator *op,
const char *path,
@@ -3751,6 +3802,16 @@ struct opendal_error
*opendal_operator_presign_write(const struct opendal_operat
* writes the request to `*req` and returns NULL; free the request with
* `opendal_presigned_request_free()`. On failure, writes NULL to `*req` and
* returns an error that must be freed with `opendal_error_free()`.
+ *
+ * # Safety
+ *
+ * * If `opts` is not NULL, each non-NULL string member of `*opts` must point
to a
+ * readable, nul-terminated C string until this function returns. A non-NULL
+ * `user_metadata` must point to `user_metadata_len` readable pairs, and
each non-NULL
+ * key and value in them to a readable, nul-terminated C string, until this
function
+ * returns. A string that is not valid UTF-8 makes this function panic. The
+ * `opendal_write_options` string and user metadata setters store the
caller's
+ * pointers, not copies.
*/
struct opendal_error *opendal_operator_presign_write_with(const struct
opendal_operator *op,
const char *path,
@@ -3764,6 +3825,16 @@ struct opendal_error
*opendal_operator_presign_write_with(const struct opendal_o
* Pass NULL for `opts` to use defaults, and NULL for `cancel` to run without
cancellation.
* On cancellation or failure, writes NULL to `*req` and returns an
`opendal_error *`
* (`OPENDAL_CANCELLED` on cancellation).
+ *
+ * # Safety
+ *
+ * * If `opts` is not NULL, each non-NULL string member of `*opts` must point
to a
+ * readable, nul-terminated C string until this function returns. A non-NULL
+ * `user_metadata` must point to `user_metadata_len` readable pairs, and
each non-NULL
+ * key and value in them to a readable, nul-terminated C string, until this
function
+ * returns. A string that is not valid UTF-8 makes this function panic. The
+ * `opendal_write_options` string and user metadata setters store the
caller's
+ * pointers, not copies.
*/
struct opendal_error *opendal_operator_presign_write_with_cancel(const struct
opendal_operator *op,
const char
*path,
@@ -5095,9 +5166,9 @@ void opendal_write_options_set_chunk(struct
opendal_write_options *opts, uintptr
* on the result of a later `opendal_operator_stat()` returns it.
*
* The setter stores `pairs` without copying the pairs or their strings, so
they
- * must stay valid until the operation that uses `opts` returns. A pair whose
`key`
- * or `value` is NULL is skipped, and a later pair replaces an earlier pair
with
- * the same key.
+ * must stay valid until a function that takes `opts`, such as
+ * `opendal_operator_write_with()`, returns. A pair whose `key` or `value` is
NULL
+ * is skipped, and a later pair replaces an earlier pair with the same key.
*
* @param opts The opendal_write_options to modify.
* @param pairs The pairs to borrow; NULL unsets it.
diff --git a/bindings/c/src/operator.rs b/bindings/c/src/operator.rs
index 89da4eb1dd..f7bbaba745 100644
--- a/bindings/c/src/operator.rs
+++ b/bindings/c/src/operator.rs
@@ -356,6 +356,16 @@ pub unsafe extern "C" fn opendal_operator_write(
}
/// \brief Blocking write raw bytes to `path` with options.
+///
+/// # Safety
+///
+/// * If `opts` is not NULL, each non-NULL string member of `*opts` must point
to a
+/// readable, nul-terminated C string until this function returns. A non-NULL
+/// `user_metadata` must point to `user_metadata_len` readable pairs, and
each non-NULL
+/// key and value in them to a readable, nul-terminated C string, until this
function
+/// returns. A string that is not valid UTF-8 makes this function panic. The
+/// `opendal_write_options` string and user metadata setters store the
caller's
+/// pointers, not copies.
#[no_mangle]
pub unsafe extern "C" fn opendal_operator_write_with(
op: &opendal_operator,
@@ -398,6 +408,13 @@ pub unsafe extern "C" fn opendal_operator_write_with(
/// * The `bytes` provided has valid byte in the `data` field and the `len`
field is set
/// correctly.
/// * `meta` must be a valid, aligned, writable pointer.
+/// * If `opts` is not NULL, each non-NULL string member of `*opts` must point
to a
+/// readable, nul-terminated C string until this function returns. A non-NULL
+/// `user_metadata` must point to `user_metadata_len` readable pairs, and
each non-NULL
+/// key and value in them to a readable, nul-terminated C string, until this
function
+/// returns. A string that is not valid UTF-8 makes this function panic. The
+/// `opendal_write_options` string and user metadata setters store the
caller's
+/// pointers, not copies.
///
/// # Panic
///
@@ -428,6 +445,16 @@ pub unsafe extern "C" fn
opendal_operator_write_with_metadata(
/// @param cancel Optional cancel token, or NULL
/// @param meta Optional output pointer receiving the allocated
`opendal_metadata *`, or NULL
/// @return NULL if the operation succeeds, or an `opendal_error *` on
cancellation or failure.
+///
+/// # Safety
+///
+/// * If `opts` is not NULL, each non-NULL string member of `*opts` must point
to a
+/// readable, nul-terminated C string until this function returns. A non-NULL
+/// `user_metadata` must point to `user_metadata_len` readable pairs, and
each non-NULL
+/// key and value in them to a readable, nul-terminated C string, until this
function
+/// returns. A string that is not valid UTF-8 makes this function panic. The
+/// `opendal_write_options` string and user metadata setters store the
caller's
+/// pointers, not copies.
#[no_mangle]
pub unsafe extern "C" fn opendal_operator_write_with_cancel(
op: &opendal_operator,
@@ -792,6 +819,18 @@ pub unsafe extern "C" fn opendal_operator_writer(
}
/// \brief Blocking create a writer for the specified path with options.
+///
+/// # Safety
+///
+/// * If `opts` is not NULL, each non-NULL string member of `*opts` must point
to a
+/// readable, nul-terminated C string until this function returns. A non-NULL
+/// `user_metadata` must point to `user_metadata_len` readable pairs, and
each non-NULL
+/// key and value in them to a readable, nul-terminated C string, until this
function
+/// returns. A string that is not valid UTF-8 makes this function panic. The
+/// `opendal_write_options` string and user metadata setters store the
caller's
+/// pointers, not copies.
+/// The returned writer does not read `*opts`, so the caller can free the
options,
+/// their strings, and the pairs when this function returns.
#[no_mangle]
pub unsafe extern "C" fn opendal_operator_writer_with(
op: &opendal_operator,
@@ -807,6 +846,18 @@ pub unsafe extern "C" fn opendal_operator_writer_with(
/// Pass NULL for `opts` to use defaults, and NULL for `cancel` to run without
cancellation.
/// On cancellation or failure, writes NULL to `*writer` and returns an
`opendal_error *`
/// (`OPENDAL_CANCELLED` on cancellation).
+///
+/// # Safety
+///
+/// * If `opts` is not NULL, each non-NULL string member of `*opts` must point
to a
+/// readable, nul-terminated C string until this function returns. A non-NULL
+/// `user_metadata` must point to `user_metadata_len` readable pairs, and
each non-NULL
+/// key and value in them to a readable, nul-terminated C string, until this
function
+/// returns. A string that is not valid UTF-8 makes this function panic. The
+/// `opendal_write_options` string and user metadata setters store the
caller's
+/// pointers, not copies.
+/// The returned writer does not read `*opts`, so the caller can free the
options,
+/// their strings, and the pairs when this function returns.
#[no_mangle]
pub unsafe extern "C" fn opendal_operator_writer_with_cancel(
op: &opendal_operator,
diff --git a/bindings/c/src/options.rs b/bindings/c/src/options.rs
index 231b82233a..3db880c421 100644
--- a/bindings/c/src/options.rs
+++ b/bindings/c/src/options.rs
@@ -2413,7 +2413,7 @@ pub struct opendal_write_options {
/// NULL or a `user_metadata_len` of `0` means unset.
/// `opendal_write_options_set_user_metadata()` stores this pointer
without copying
/// the pairs or their strings, and `opendal_write_options_free()` does
not free
- /// them, so they must stay valid until the operation that uses these
options
+ /// them, so they must stay valid until the function that takes these
options
/// returns. A pair whose `key` or `value` is NULL is skipped, and a later
pair
/// replaces an earlier pair with the same key.
pub user_metadata: *const opendal_write_user_metadata_pair,
@@ -2823,9 +2823,9 @@ impl opendal_write_options {
/// on the result of a later `opendal_operator_stat()` returns it.
///
/// The setter stores `pairs` without copying the pairs or their strings,
so they
- /// must stay valid until the operation that uses `opts` returns. A pair
whose `key`
- /// or `value` is NULL is skipped, and a later pair replaces an earlier
pair with
- /// the same key.
+ /// must stay valid until a function that takes `opts`, such as
+ /// `opendal_operator_write_with()`, returns. A pair whose `key` or
`value` is NULL
+ /// is skipped, and a later pair replaces an earlier pair with the same
key.
///
/// @param opts The opendal_write_options to modify.
/// @param pairs The pairs to borrow; NULL unsets it.
diff --git a/bindings/c/src/presign.rs b/bindings/c/src/presign.rs
index ff3766c424..bb75feba86 100644
--- a/bindings/c/src/presign.rs
+++ b/bindings/c/src/presign.rs
@@ -252,6 +252,16 @@ pub unsafe extern "C" fn opendal_operator_presign_write(
/// writes the request to `*req` and returns NULL; free the request with
/// `opendal_presigned_request_free()`. On failure, writes NULL to `*req` and
/// returns an error that must be freed with `opendal_error_free()`.
+///
+/// # Safety
+///
+/// * If `opts` is not NULL, each non-NULL string member of `*opts` must point
to a
+/// readable, nul-terminated C string until this function returns. A non-NULL
+/// `user_metadata` must point to `user_metadata_len` readable pairs, and
each non-NULL
+/// key and value in them to a readable, nul-terminated C string, until this
function
+/// returns. A string that is not valid UTF-8 makes this function panic. The
+/// `opendal_write_options` string and user metadata setters store the
caller's
+/// pointers, not copies.
#[no_mangle]
pub unsafe extern "C" fn opendal_operator_presign_write_with(
op: &opendal_operator,
@@ -268,6 +278,16 @@ pub unsafe extern "C" fn
opendal_operator_presign_write_with(
/// Pass NULL for `opts` to use defaults, and NULL for `cancel` to run without
cancellation.
/// On cancellation or failure, writes NULL to `*req` and returns an
`opendal_error *`
/// (`OPENDAL_CANCELLED` on cancellation).
+///
+/// # Safety
+///
+/// * If `opts` is not NULL, each non-NULL string member of `*opts` must point
to a
+/// readable, nul-terminated C string until this function returns. A non-NULL
+/// `user_metadata` must point to `user_metadata_len` readable pairs, and
each non-NULL
+/// key and value in them to a readable, nul-terminated C string, until this
function
+/// returns. A string that is not valid UTF-8 makes this function panic. The
+/// `opendal_write_options` string and user metadata setters store the
caller's
+/// pointers, not copies.
#[no_mangle]
pub unsafe extern "C" fn opendal_operator_presign_write_with_cancel(
op: &opendal_operator,
diff --git a/bindings/c/src/types.rs b/bindings/c/src/types.rs
index f07d85cac3..373a444674 100644
--- a/bindings/c/src/types.rs
+++ b/bindings/c/src/types.rs
@@ -30,11 +30,11 @@ use opendal::{Error, ErrorKind};
#[repr(C)]
pub struct opendal_write_user_metadata_pair {
/// The metadata key. A non-null key must point to a nul-terminated UTF-8
- /// string that stays valid until the operation that uses the options
+ /// string that stays valid until the function that takes the options
/// returns.
pub key: *const c_char,
/// The metadata value. A non-null value must point to a nul-terminated
- /// UTF-8 string that stays valid until the operation that uses the options
+ /// UTF-8 string that stays valid until the function that takes the options
/// returns.
pub value: *const c_char,
}
diff --git a/dev/src/generate/c/options.rs b/dev/src/generate/c/options.rs
index 75a0303281..6d49f2e884 100644
--- a/dev/src/generate/c/options.rs
+++ b/dev/src/generate/c/options.rs
@@ -861,13 +861,13 @@ fn options_model(core: &OptionsStruct, spec:
&OptionsSpec, names: &Names) -> Res
Some(format!(
"NULL or a `{name}_len` of `0` means unset. `{setter}`
stores this pointer \
without copying the pairs or their strings, and
`{c_name}_free()` does \
- not free them, so they must stay valid until the
operation that uses \
+ not free them, so they must stay valid until the function
that takes \
these options returns. {pairs_rule}"
)),
Some(format!(
"The setter stores `pairs` without copying the pairs or
their strings, so \
- they must stay valid until the operation that uses `opts`
returns. \
- {pairs_rule}"
+ they must stay valid until a function that takes `opts`,
such as \
+ `opendal_operator_{base}_with()`, returns. {pairs_rule}"
)),
"",
),
@@ -2866,7 +2866,7 @@ mod tests {
"NULL or a `user_metadata_len` of `0` means unset. \
`opendal_write_options_set_user_metadata()` stores this
pointer without \
copying the pairs or their strings, and
`opendal_write_options_free()` does \
- not free them, so they must stay valid until the operation
that uses these \
+ not free them, so they must stay valid until the function
that takes these \
options returns. {rules}"
)),
"{member}"
@@ -2883,7 +2883,8 @@ mod tests {
assert!(
setter.ends_with(&format!(
"The setter stores `pairs` without copying the pairs or their
strings, so \
- they must stay valid until the operation that uses `opts`
returns. {rules} \
+ they must stay valid until a function that takes `opts`, such
as \
+ `opendal_operator_write_with()`, returns. {rules} \
@param opts The opendal_write_options to modify. \
@param pairs The pairs to borrow; NULL unsets it. \
@param len The number of pairs at `pairs`; `0` unsets it."