diff mbox series

[2/3] lib/charset: add u16_strcat() function

Message ID 20220210070537.28667-3-masahisa.kojima@linaro.org
State New
Headers show
Series enable menu-driven boot device selection | expand

Commit Message

Masahisa Kojima Feb. 10, 2022, 7:05 a.m. UTC
Provide u16 string version of strcat().

Signed-off-by: Masahisa Kojima <masahisa.kojima@linaro.org>
---
 include/charset.h | 13 +++++++++++++
 lib/charset.c     | 12 ++++++++++++
 2 files changed, 25 insertions(+)

Comments

Heinrich Schuchardt Feb. 13, 2022, 10:12 a.m. UTC | #1
On 2/10/22 08:05, Masahisa Kojima wrote:
> Provide u16 string version of strcat().
>
> Signed-off-by: Masahisa Kojima <masahisa.kojima@linaro.org>

Please, provide a test in test/unicode_ut.c.
> ---
>   include/charset.h | 13 +++++++++++++
>   lib/charset.c     | 12 ++++++++++++
>   2 files changed, 25 insertions(+)
>
> diff --git a/include/charset.h b/include/charset.h
> index b93d023092..baba9d7c14 100644
> --- a/include/charset.h
> +++ b/include/charset.h
> @@ -259,6 +259,19 @@ u16 *u16_strcpy(u16 *dest, const u16 *src);
>    */
>   u16 *u16_strdup(const void *src);
>
> +/**
> + * u16_strcat() - append u16 string
> + *
> + * Append the src string to the dest string, overwriting the terminating
> + * null word at the end of dest, and then adds a terminating null word.
> + * The dest string must have enough space for the result.
> + *
> + * @dest:		destination buffer (null terminated)
> + * @src:		source buffer (null terminated)
> + * Return:		'dest' address
> + */
> +u16 *u16_strcat(u16 *dest, const u16 *src);

This is unsafe. Please, provide an argument for the destination buffer
size. If you still need a version without the argument, simply use a
define like we did for other functions.

Best regards

Heinrich

> +
>   /**
>    * utf16_to_utf8() - Convert an utf16 string to utf8
>    *
> diff --git a/lib/charset.c b/lib/charset.c
> index f44c58d9d8..f0eaf6c1ae 100644
> --- a/lib/charset.c
> +++ b/lib/charset.c
> @@ -428,6 +428,18 @@ u16 *u16_strdup(const void *src)
>   	return new;
>   }
>
> +u16 *u16_strcat(u16 *dest, const u16 *src)
> +{
> +	u16 *tmp = dest;
> +
> +	while (*dest)
> +		dest++;
> +	while ((*dest++ = *src++) != u'\0')
> +		;
> +
> +	return tmp;
> +}
> +
>   /* Convert UTF-16 to UTF-8.  */
>   uint8_t *utf16_to_utf8(uint8_t *dest, const uint16_t *src, size_t size)
>   {
Masahisa Kojima Feb. 14, 2022, 1:51 a.m. UTC | #2
Hi Heinrich,

On Sun, 13 Feb 2022 at 19:12, Heinrich Schuchardt <xypron.glpk@gmx.de> wrote:
>
> On 2/10/22 08:05, Masahisa Kojima wrote:
> > Provide u16 string version of strcat().
> >
> > Signed-off-by: Masahisa Kojima <masahisa.kojima@linaro.org>
>
> Please, provide a test in test/unicode_ut.c.

Yes, I will add.

> > ---
> >   include/charset.h | 13 +++++++++++++
> >   lib/charset.c     | 12 ++++++++++++
> >   2 files changed, 25 insertions(+)
> >
> > diff --git a/include/charset.h b/include/charset.h
> > index b93d023092..baba9d7c14 100644
> > --- a/include/charset.h
> > +++ b/include/charset.h
> > @@ -259,6 +259,19 @@ u16 *u16_strcpy(u16 *dest, const u16 *src);
> >    */
> >   u16 *u16_strdup(const void *src);
> >
> > +/**
> > + * u16_strcat() - append u16 string
> > + *
> > + * Append the src string to the dest string, overwriting the terminating
> > + * null word at the end of dest, and then adds a terminating null word.
> > + * The dest string must have enough space for the result.
> > + *
> > + * @dest:            destination buffer (null terminated)
> > + * @src:             source buffer (null terminated)
> > + * Return:           'dest' address
> > + */
> > +u16 *u16_strcat(u16 *dest, const u16 *src);
>
> This is unsafe. Please, provide an argument for the destination buffer
> size. If you still need a version without the argument, simply use a
> define like we did for other functions.

OK, I will create u16_strcat_s() instead of u16_strcat().

Thank you for your review.

Regards,
Masahisa Kojima

>
> Best regards
>
> Heinrich
>
> > +
> >   /**
> >    * utf16_to_utf8() - Convert an utf16 string to utf8
> >    *
> > diff --git a/lib/charset.c b/lib/charset.c
> > index f44c58d9d8..f0eaf6c1ae 100644
> > --- a/lib/charset.c
> > +++ b/lib/charset.c
> > @@ -428,6 +428,18 @@ u16 *u16_strdup(const void *src)
> >       return new;
> >   }
> >
> > +u16 *u16_strcat(u16 *dest, const u16 *src)
> > +{
> > +     u16 *tmp = dest;
> > +
> > +     while (*dest)
> > +             dest++;
> > +     while ((*dest++ = *src++) != u'\0')
> > +             ;
> > +
> > +     return tmp;
> > +}
> > +
> >   /* Convert UTF-16 to UTF-8.  */
> >   uint8_t *utf16_to_utf8(uint8_t *dest, const uint16_t *src, size_t size)
> >   {
>
diff mbox series

Patch

diff --git a/include/charset.h b/include/charset.h
index b93d023092..baba9d7c14 100644
--- a/include/charset.h
+++ b/include/charset.h
@@ -259,6 +259,19 @@  u16 *u16_strcpy(u16 *dest, const u16 *src);
  */
 u16 *u16_strdup(const void *src);
 
+/**
+ * u16_strcat() - append u16 string
+ *
+ * Append the src string to the dest string, overwriting the terminating
+ * null word at the end of dest, and then adds a terminating null word.
+ * The dest string must have enough space for the result.
+ *
+ * @dest:		destination buffer (null terminated)
+ * @src:		source buffer (null terminated)
+ * Return:		'dest' address
+ */
+u16 *u16_strcat(u16 *dest, const u16 *src);
+
 /**
  * utf16_to_utf8() - Convert an utf16 string to utf8
  *
diff --git a/lib/charset.c b/lib/charset.c
index f44c58d9d8..f0eaf6c1ae 100644
--- a/lib/charset.c
+++ b/lib/charset.c
@@ -428,6 +428,18 @@  u16 *u16_strdup(const void *src)
 	return new;
 }
 
+u16 *u16_strcat(u16 *dest, const u16 *src)
+{
+	u16 *tmp = dest;
+
+	while (*dest)
+		dest++;
+	while ((*dest++ = *src++) != u'\0')
+		;
+
+	return tmp;
+}
+
 /* Convert UTF-16 to UTF-8.  */
 uint8_t *utf16_to_utf8(uint8_t *dest, const uint16_t *src, size_t size)
 {