[dm-devel] [PATCH v2 12/35] libmultipath: strlcpy()/strlcat(): use restrict qualifier
Benjamin Marzinski
bmarzins at redhat.com
Wed Aug 12 18:01:17 UTC 2020
On Wed, Aug 12, 2020 at 01:32:32PM +0200, mwilck at suse.com wrote:
> From: Martin Wilck <mwilck at suse.com>
>
> Also remove the redundant local variables. It's not necessary to
> make "restrict" work, but it makes the intention more clear.
>
Clearly, the way you wrote strlcat agrees with other implementations,
so it's fine.
Reviewed-by: Benjamin Marzinski <bmarzins at redhat.com>
> Signed-off-by: Martin Wilck <mwilck at suse.com>
> ---
> libmultipath/util.c | 28 ++++++++++++----------------
> libmultipath/util.h | 4 ++--
> 2 files changed, 14 insertions(+), 18 deletions(-)
>
> diff --git a/libmultipath/util.c b/libmultipath/util.c
> index 526869e..207e63c 100644
> --- a/libmultipath/util.c
> +++ b/libmultipath/util.c
> @@ -113,46 +113,42 @@ get_word (const char *sentence, char **word)
> return skip + len;
> }
>
> -size_t strlcpy(char *dst, const char *src, size_t size)
> +size_t strlcpy(char * restrict dst, const char * restrict src, size_t size)
> {
> size_t bytes = 0;
> - char *q = dst;
> - const char *p = src;
> char ch;
>
> - while ((ch = *p++)) {
> - if (bytes+1 < size)
> - *q++ = ch;
> + while ((ch = *src++)) {
> + if (bytes + 1 < size)
> + *dst++ = ch;
> bytes++;
> }
>
> /* If size == 0 there is no space for a final null... */
> if (size)
> - *q = '\0';
> + *dst = '\0';
> return bytes;
> }
>
> -size_t strlcat(char *dst, const char *src, size_t size)
> +size_t strlcat(char * restrict dst, const char * restrict src, size_t size)
> {
> size_t bytes = 0;
> - char *q = dst;
> - const char *p = src;
> char ch;
>
> - while (bytes < size && *q) {
> - q++;
> + while (bytes < size && *dst) {
> + dst++;
> bytes++;
> }
> if (bytes == size)
> return (bytes + strlen(src));
>
> - while ((ch = *p++)) {
> - if (bytes+1 < size)
> - *q++ = ch;
> + while ((ch = *src++)) {
> + if (bytes + 1 < size)
> + *dst++ = ch;
> bytes++;
> }
>
> - *q = '\0';
> + *dst = '\0';
> return bytes;
> }
>
> diff --git a/libmultipath/util.h b/libmultipath/util.h
> index a4f7c0a..52aa559 100644
> --- a/libmultipath/util.h
> +++ b/libmultipath/util.h
> @@ -15,8 +15,8 @@ int basenamecpy (const char *src, char *dst, size_t size);
> int filepresent (const char *run);
> char *get_next_string(char **temp, const char *split_char);
> int get_word (const char * sentence, char ** word);
> -size_t strlcpy(char *dst, const char *src, size_t size);
> -size_t strlcat(char *dst, const char *src, size_t size);
> +size_t strlcpy(char * restrict dst, const char * restrict src, size_t size);
> +size_t strlcat(char * restrict dst, const char * restrict src, size_t size);
> int devt2devname (char *, int, const char *);
> dev_t parse_devt(const char *dev_t);
> char *convert_dev(char *dev, int is_path_device);
> --
> 2.28.0
More information about the dm-devel
mailing list