diff options
-rw-r--r-- | openpgp-ffi/examples/Makefile | 5 | ||||
-rw-r--r-- | openpgp-ffi/examples/immutable-reference-demo.c | 36 |
2 files changed, 40 insertions, 1 deletions
diff --git a/openpgp-ffi/examples/Makefile b/openpgp-ffi/examples/Makefile index 7986d7d6..35c4e9e8 100644 --- a/openpgp-ffi/examples/Makefile +++ b/openpgp-ffi/examples/Makefile @@ -8,7 +8,10 @@ CARGO_TARGET_DIR := $(abspath $(CARGO_TARGET_DIR)) # We are producing binaries here. EXAMPLE_TARGET_DIR ?= $(CARGO_TARGET_DIR)/debug/c-examples/openpgp-ffi -EXAMPLES = example reader parser encrypt-for armor type-safety-demo use-after-free-demo +EXAMPLES = \ + example reader parser encrypt-for armor \ + type-safety-demo use-after-free-demo immutable-reference-demo \ + CFLAGS = -I../include -O0 -g -Wall -Werror LDFLAGS = -L$(CARGO_TARGET_DIR)/debug -lsequoia_openpgp_ffi diff --git a/openpgp-ffi/examples/immutable-reference-demo.c b/openpgp-ffi/examples/immutable-reference-demo.c new file mode 100644 index 00000000..a0ada27b --- /dev/null +++ b/openpgp-ffi/examples/immutable-reference-demo.c @@ -0,0 +1,36 @@ +#define _GNU_SOURCE +#include <errno.h> +#include <error.h> +#include <fcntl.h> +#include <stdio.h> +#include <stdlib.h> +#include <sys/mman.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <unistd.h> + +#include <sequoia/openpgp.h> + +int +main (int argc, char **argv) +{ + /* Create a new TSK. */ + pgp_tsk_t tsk; + pgp_signature_t revocation; + pgp_tsk_new (NULL, "", &tsk, &revocation); + pgp_signature_free (revocation); + + /* Let's borrow a immutable reference from it. */ + pgp_tpk_t tpk = pgp_tsk_tpk (tsk); + + /* Let's violate The Rules and forge a mutable reference from + * the immutable one! */ + pgp_tpk_t tpk_mut = (pgp_tpk_t) tpk; + + /* And try to convert the TPK to a TSK, moving the ownership! */ + pgp_tpk_into_tsk (tpk_mut); + + /* Always clean up though. */ + pgp_tsk_free (tsk); + return 0; +} |