]> git.ipfire.org Git - thirdparty/collectd.git/commit
common: Overhaul the `sstrncpy` implementation. 4233/head
authorFlorian Forster <octo@collectd.org>
Fri, 12 Jan 2024 16:51:07 +0000 (17:51 +0100)
committerFlorian Forster <octo@collectd.org>
Mon, 15 Jan 2024 07:40:06 +0000 (08:40 +0100)
commitd25f08fa2981a143aeb9087f387c291984f49c7c
treee6e0eb1b57225d88161d68cac6221ea03d1fbfef
parent7de6192d5f827f0204cbf8c45ca921c3ed298cc8
common: Overhaul the `sstrncpy` implementation.

Properly check all arguments and behave in a sane manner, i.e. don't crash.

I went back and forth a few times on whether to return `NULL` or `dest` when `n == 0`.

*   On the one hand, `n == 0` is not really an error and a situation that could
    naturally occur, e.g. when you're implementing code that appends to the end
    of a string.
*   On the other hand, if we return `NULL` when `n` is zero we can guarantee
    that we will either return `NULL` or a null terminated string.

Ultimately I decided to go with the stronger guarantee, i.e.

```c
if (n == 0) {
  return NULL;
}
```
src/utils/common/common.c
src/utils/common/common.h
src/utils/common/common_test.c