From e6e077c8469f857ac58d804c788e887ce86e2303 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Andr=C3=A9=20Tanner?= Date: Sun, 10 Jan 2016 12:23:32 +0100 Subject: text: introduce text_bytes_alloc0 utility function Heap allocates a zero terminated string of the given range. Freeing is the caller's responsibility. Checks for unsigned integer overflow i.e. passing SIZE_MAX as len will always fail because there is no room for the terminating NUL byte. This is important as EPOS is defined to be SIZE_MAX. --- text.h | 3 +++ 1 file changed, 3 insertions(+) (limited to 'text.h') diff --git a/text.h b/text.h index b2783b3..90da900 100644 --- a/text.h +++ b/text.h @@ -72,6 +72,9 @@ bool text_byte_get(Text*, size_t pos, char *buf); * indicates how many bytes were copied into `buf'. WARNING buf will not be * NUL terminated. */ size_t text_bytes_get(Text*, size_t pos, size_t len, char *buf); +/* allocate a NUL terminated buffer and fill at most `len' bytes + * starting at `pos'. Freeing is the caller's responsibility! */ +char *text_bytes_alloc0(Text*, size_t pos, size_t len); Iterator text_iterator_get(Text*, size_t pos); bool text_iterator_valid(const Iterator*); -- cgit v1.2.3