Skip to content
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

Move protection #575

Merged
merged 22 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from 18 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
2 changes: 1 addition & 1 deletion examples/arduino/z_get.ino
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ void loop() {
z_owned_bytes_t payload;
if (strcmp(VALUE, "") != 0) {
z_bytes_from_static_str(&payload, VALUE);
opts.payload = &payload;
opts.payload = z_bytes_move(&payload);
}
z_owned_closure_reply_t callback;
z_closure_reply(&callback, reply_handler, reply_dropper, NULL);
Expand Down
2 changes: 1 addition & 1 deletion examples/espidf/z_get.c
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ void app_main() {
z_owned_bytes_t payload;
if (strcmp(VALUE, "") != 0) {
z_bytes_from_static_str(&payload, VALUE);
opts.payload = &payload;
opts.payload = z_move(payload);
}
z_owned_closure_reply_t callback;
z_closure(&callback, reply_handler, reply_dropper);
Expand Down
4 changes: 2 additions & 2 deletions examples/freertos_plus_tcp/z_get.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ void app_main(void) {
z_view_keyexpr_t ke;
if (z_view_keyexpr_from_str(&ke, KEYEXPR) < 0) {
printf("%s is not a valid key expression", KEYEXPR);
return -1;
return;
}

while (1) {
Expand All @@ -89,7 +89,7 @@ void app_main(void) {
z_owned_bytes_t payload;
if (strcmp(VALUE, "") != 0) {
z_bytes_from_static_str(&payload, VALUE);
opts.payload = &payload;
opts.payload = z_move(payload);
}
z_owned_closure_reply_t callback;
z_closure(&callback, reply_handler, reply_dropper);
Expand Down
2 changes: 1 addition & 1 deletion examples/freertos_plus_tcp/z_queryable.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ void app_main(void) {
z_view_keyexpr_t ke;
if (z_view_keyexpr_from_str(&ke, KEYEXPR) < 0) {
printf("%s is not a valid key expression", KEYEXPR);
return -1;
return;
}

printf("Creating Queryable on '%s'...\n", KEYEXPR);
Expand Down
2 changes: 1 addition & 1 deletion examples/mbed/z_get.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ int main(int argc, char **argv) {
z_owned_bytes_t payload;
if (strcmp(VALUE, "") != 0) {
z_bytes_from_static_str(&payload, VALUE);
opts.payload = &payload;
opts.payload = z_bytes_move(&payload);
}
z_owned_closure_reply_t callback;
z_closure_reply(&callback, reply_handler, reply_dropper, NULL);
Expand Down
2 changes: 1 addition & 1 deletion examples/unix/c11/z_get.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ int main(int argc, char **argv) {
z_owned_bytes_t payload;
if (value != NULL) {
z_bytes_from_static_str(&payload, value);
opts.payload = &payload;
opts.payload = z_bytes_move(&payload);
}

z_owned_closure_reply_t callback;
Expand Down
12 changes: 6 additions & 6 deletions examples/unix/c11/z_get_attachment.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ void parse_attachment(kv_pairs_rx_t *kvp, const z_loaned_bytes_t *attachment) {
z_bytes_deserialize_into_pair(z_loan(kv), &first, &second);
z_bytes_deserialize_into_string(z_loan(first), &kvp->data[kvp->current_idx].key);
z_bytes_deserialize_into_string(z_loan(second), &kvp->data[kvp->current_idx].value);
z_bytes_drop(&first);
z_bytes_drop(&second);
z_bytes_drop(&kv);
z_bytes_drop(z_bytes_move(&first));
z_bytes_drop(z_bytes_move(&second));
z_bytes_drop(z_bytes_move(&kv));
kvp->current_idx++;
}
}
Expand All @@ -85,8 +85,8 @@ void print_attachment(kv_pairs_rx_t *kvp) {

void drop_attachment(kv_pairs_rx_t *kvp) {
for (size_t i = 0; i < kvp->current_idx; i++) {
z_string_drop(&kvp->data[i].key);
z_string_drop(&kvp->data[i].value);
z_string_drop(z_string_move(&kvp->data[i].key));
z_string_drop(z_string_move(&kvp->data[i].value));
}
z_free(kvp->data);
}
Expand Down Expand Up @@ -206,7 +206,7 @@ int main(int argc, char **argv) {
z_owned_bytes_t payload;
if (value != NULL) {
z_bytes_from_static_str(&payload, value);
opts.payload = &payload;
opts.payload = z_bytes_move(&payload);
}

// Add attachment value
Expand Down
2 changes: 1 addition & 1 deletion examples/unix/c11/z_get_channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ int main(int argc, char **argv) {
z_owned_bytes_t payload;
if (value != NULL) {
z_bytes_from_static_str(&payload, value);
opts.payload = &payload;
opts.payload = z_bytes_move(&payload);
}
z_owned_closure_reply_t closure;
z_owned_ring_handler_reply_t handler;
Expand Down
4 changes: 2 additions & 2 deletions examples/unix/c11/z_pong.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void callback(const z_loaned_sample_t* sample, void* context) {
}
void drop(void* context) {
z_owned_publisher_t* pub = (z_owned_publisher_t*)context;
z_drop(pub);
z_drop(z_move(*pub));
// A note on lifetimes:
// here, `sub` takes ownership of `pub` and will drop it before returning from its own `drop`,
// which makes passing a pointer to the stack safe as long as `sub` is dropped in a scope where `pub` is still
Expand Down Expand Up @@ -59,7 +59,7 @@ int main(int argc, char** argv) {
z_view_keyexpr_t ping;
z_view_keyexpr_from_str_unchecked(&ping, "test/ping");
z_owned_closure_sample_t respond;
z_closure(&respond, callback, drop, (void*)z_move(pub));
z_closure(&respond, callback, drop, (void*)(&pub));
z_owned_subscriber_t sub;
if (z_declare_subscriber(&sub, z_loan(session), z_loan(ping), z_move(respond), NULL) < 0) {
printf("Unable to declare subscriber for key expression.\n");
Expand Down
10 changes: 5 additions & 5 deletions examples/unix/c11/z_queryable_attachment.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ void parse_attachment(kv_pairs_rx_t *kvp, const z_loaned_bytes_t *attachment) {
z_bytes_deserialize_into_pair(z_loan(kv), &first, &second);
z_bytes_deserialize_into_string(z_loan(first), &kvp->data[kvp->current_idx].key);
z_bytes_deserialize_into_string(z_loan(second), &kvp->data[kvp->current_idx].value);
z_bytes_drop(&first);
z_bytes_drop(&second);
z_bytes_drop(&kv);
z_bytes_drop(z_bytes_move(&first));
z_bytes_drop(z_bytes_move(&second));
z_bytes_drop(z_bytes_move(&kv));
kvp->current_idx++;
}
}
Expand All @@ -97,8 +97,8 @@ void print_attachment(kv_pairs_rx_t *kvp) {

void drop_attachment(kv_pairs_rx_t *kvp) {
for (size_t i = 0; i < kvp->current_idx; i++) {
z_string_drop(&kvp->data[i].key);
z_string_drop(&kvp->data[i].value);
z_string_drop(z_string_move(&kvp->data[i].key));
z_string_drop(z_string_move(&kvp->data[i].value));
}
z_free(kvp->data);
}
Expand Down
10 changes: 5 additions & 5 deletions examples/unix/c11/z_sub_attachment.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ void parse_attachment(kv_pairs_t *kvp, const z_loaned_bytes_t *attachment) {
z_bytes_deserialize_into_pair(z_loan(kv), &first, &second);
z_bytes_deserialize_into_string(z_loan(first), &kvp->data[kvp->current_idx].key);
z_bytes_deserialize_into_string(z_loan(second), &kvp->data[kvp->current_idx].value);
z_bytes_drop(&first);
z_bytes_drop(&second);
z_bytes_drop(&kv);
z_bytes_drop(z_bytes_move(&first));
z_bytes_drop(z_bytes_move(&second));
z_bytes_drop(z_bytes_move(&kv));
kvp->current_idx++;
}
}
Expand All @@ -63,8 +63,8 @@ void print_attachment(kv_pairs_t *kvp) {

void drop_attachment(kv_pairs_t *kvp) {
for (size_t i = 0; i < kvp->current_idx; i++) {
z_string_drop(&kvp->data[i].key);
z_string_drop(&kvp->data[i].value);
z_string_drop(z_string_move(&kvp->data[i].key));
z_string_drop(z_string_move(&kvp->data[i].value));
}
z_free(kvp->data);
}
Expand Down
2 changes: 1 addition & 1 deletion examples/unix/c99/z_get.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ int main(int argc, char **argv) {
z_owned_bytes_t payload;
if (value != NULL) {
z_bytes_from_static_str(&payload, value);
opts.payload = &payload;
opts.payload = z_bytes_move(&payload);
}
z_owned_closure_reply_t callback;
z_closure_reply(&callback, reply_handler, reply_dropper, NULL);
Expand Down
4 changes: 2 additions & 2 deletions examples/unix/c99/z_pong.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ void callback(const z_loaned_sample_t* sample, void* context) {

void drop(void* context) {
z_owned_publisher_t* pub = (z_owned_publisher_t*)context;
z_undeclare_publisher(pub);
z_undeclare_publisher(z_publisher_move(pub));
// A note on lifetimes:
// here, `sub` takes ownership of `pub` and will drop it before returning from its own `drop`,
// which makes passing a pointer to the stack safe as long as `sub` is dropped in a scope where `pub` is still
Expand Down Expand Up @@ -63,7 +63,7 @@ int main(int argc, char** argv) {
z_view_keyexpr_t ping;
z_view_keyexpr_from_str_unchecked(&ping, "test/ping");
z_owned_closure_sample_t respond;
z_closure_sample(&respond, callback, drop, (void*)z_publisher_move(&pub));
z_closure_sample(&respond, callback, drop, (void*)(&pub));
z_owned_subscriber_t sub;
if (z_declare_subscriber(&sub, z_session_loan(&session), z_view_keyexpr_loan(&ping),
z_closure_sample_move(&respond), NULL) < 0) {
Expand Down
2 changes: 1 addition & 1 deletion examples/windows/z_get.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ int main(int argc, char **argv) {
z_owned_bytes_t payload;
if (value != NULL) {
z_bytes_from_static_str(&payload, value);
opts.payload = &payload;
opts.payload = z_move(payload);
}
z_owned_closure_reply_t callback;
z_closure(&callback, reply_handler, reply_dropper);
Expand Down
4 changes: 2 additions & 2 deletions examples/windows/z_pong.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void callback(const z_loaned_sample_t* sample, void* context) {
}
void drop(void* context) {
z_owned_publisher_t* pub = (z_owned_publisher_t*)context;
z_drop(pub);
z_drop(z_move(*pub));
// A note on lifetimes:
// here, `sub` takes ownership of `pub` and will drop it before returning from its own `drop`,
// which makes passing a pointer to the stack safe as long as `sub` is dropped in a scope where `pub` is still
Expand Down Expand Up @@ -59,7 +59,7 @@ int main(int argc, char** argv) {
z_view_keyexpr_t ping;
z_view_keyexpr_from_str_unchecked(&ping, "test/ping");
z_owned_closure_sample_t respond;
z_closure(&respond, callback, drop, (void*)z_move(pub));
z_closure(&respond, callback, drop, (void*)(&pub));
z_owned_subscriber_t sub;
if (z_declare_subscriber(&sub, z_loan(session), z_loan(ping), z_move(respond), NULL) < 0) {
printf("Unable to declare subscriber for key expression.\n");
Expand Down
2 changes: 1 addition & 1 deletion examples/zephyr/z_get.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ int main(int argc, char **argv) {
z_owned_bytes_t payload;
if (strcmp(VALUE, "") != 0) {
z_bytes_from_static_str(&payload, VALUE);
opts.payload = &payload;
opts.payload = z_move(payload);
}
z_owned_closure_reply_t callback;
z_closure(&callback, reply_handler, reply_dropper);
Expand Down
32 changes: 18 additions & 14 deletions include/zenoh-pico/api/handlers.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,15 @@ extern "C" {
#define _Z_CHANNEL_DEFINE_IMPL(handler_type, handler_name, handler_new_f_name, callback_type, callback_new_f, \
collection_type, collection_new_f, collection_free_f, collection_push_f, \
collection_pull_f, collection_try_pull_f, collection_close_f, elem_owned_type, \
elem_loaned_type, elem_clone_f, elem_drop_f, elem_null_f) \
elem_loaned_type, elem_clone_f, elem_move_f, elem_drop_f, elem_null_f) \
typedef struct { \
collection_type *collection; \
} handler_type; \
\
_Z_OWNED_TYPE_VALUE(handler_type, handler_name) \
_Z_LOANED_TYPE(handler_type, handler_name) \
\
static inline void _z_##handler_name##_elem_free(void **elem) { \
elem_drop_f((elem_owned_type *)*elem); \
elem_drop_f(elem_move_f((elem_owned_type *)*elem)); \
z_free(*elem); \
*elem = NULL; \
} \
Expand Down Expand Up @@ -140,20 +139,25 @@ extern "C" {
/* elem_owned_type */ z_owned_##item_name##_t, \
/* elem_loaned_type */ z_loaned_##item_name##_t, \
/* elem_clone_f */ z_##item_name##_clone, \
/* elem_move_f */ z_##item_name##_move, \
/* elem_drop_f */ z_##item_name##_drop, \
/* elem_null */ z_##item_name##_null)

#define _Z_CHANNEL_DEFINE_DUMMY(item_name, kind_name) \
typedef struct { \
uint8_t _foo; \
} z_owned_##kind_name##_handler_##item_name##_t; \
typedef struct { \
uint8_t _foo; \
} z_loaned_##kind_name##_handler_##item_name##_t; \
void *z_##kind_name##_handler_##item_name##_loan(void); \
void *z_##kind_name##_handler_##item_name##_move(void); \
void *z_##kind_name##_handler_##item_name##_drop(void); \
void *z_##kind_name##_handler_##item_name##_recv(void); \
#define _Z_CHANNEL_DEFINE_DUMMY(item_name, kind_name) \
typedef struct { \
uint8_t _foo; \
} z_owned_##kind_name##_handler_##item_name##_t; \
typedef struct { \
uint8_t _foo; \
} z_loaned_##kind_name##_handler_##item_name##_t; \
typedef struct { \
z_owned_##kind_name##_handler_##item_name##_t *_ptr; \
} z_moved_##kind_name##_handler_##item_name##_t; \
void *z_##kind_name##_handler_##item_name##_loan(void); \
void *z_##kind_name##_handler_##item_name##_move(void); \
void *z_##kind_name##_handler_##item_name##_drop(void); \
void *z_##kind_name##_handler_##item_name##_recv(void); \
void *z_##kind_name##_handler_##item_name##_take(void); \
void *z_##kind_name##_handler_##item_name##_try_recv(void);

// This macro defines:
Expand Down
Loading
Loading