I have a function which receives a buffer and returns some data in the buffer. The data can be smaller than the buffer capacity.
Which is the best and safest API for this?
int fn(void *buffer, size_t buffer_len)
: the size of the data written tobuffer
is returned by the function. Downside: the return value must also have a way to indicate that some error occurred (in-band error indicator).errno_t fn(void *buffer, size_t *buffer_len)
: in this case,buffer_len
works both as input (the buffer capacity) and output (the data size). The function can return an error code. I think this is OK, but somewhat awkward.errno_t fn(void *buffer, size_t *data_len, size_t buffer_len)
: like the previous, but with input/output separated in two arguments. Also returns error code, but is also awkward due to too many arguments.
(Any other options?)