pkarashchenko commented on code in PR #14137:
URL: https://github.com/apache/nuttx/pull/14137#discussion_r1798604448


##########
libs/libc/string/lib_strdup.c:
##########
@@ -47,3 +47,16 @@ FAR char *strdup(FAR const char *s)
 
   return news;
 }
+
+FAR char *nx_strdup(FAR const char *s)
+{
+  size_t size = strlen(s) + 1;
+  FAR char *news = (FAR char *)kmm_malloc(size);
+
+  if (news)

Review Comment:
   Optional
   ```suggestion
     if (news != NULL)
   ```



##########
libs/libc/string/lib_strndup.c:
##########
@@ -34,6 +34,47 @@
  * Public Functions
  ****************************************************************************/
 
+/****************************************************************************
+ * Name: nx_strndup
+ *
+ * Description:
+ *   The strndup() function is equivalent to the strdup() function,
+ *   duplicating the provided 's' in a new block of memory allocated as
+ *   if by using kmm_malloc(), with the exception being that strndup() copies
+ *   at most 'size' plus one bytes into the newly allocated memory,
+ *   terminating the new string with a NUL character. If the length of 's'
+ *   is larger than 'size', only 'size' bytes will be duplicated. If
+ *   'size' is larger than the length of 's', all bytes in s will be
+ *   copied into the new memory buffer, including the terminating NUL
+ *   character. The newly created string will always be properly
+ *   terminated.
+ *
+ ****************************************************************************/
+
+FAR char *nx_strndup(FAR const char *s, size_t size)
+{
+  FAR char *news = NULL;

Review Comment:
   Minor: `NULL` init is redundant since is overwritten unconditionally few 
lines below



##########
libs/libc/stdio/lib_vasprintf.c:
##########
@@ -34,6 +34,101 @@
  * Public Functions
  ****************************************************************************/
 
+/****************************************************************************
+ * Name: nx_vasprintf
+ *
+ * Description:
+ *   This function is similar to vsprintf, except that it dynamically
+ *   allocates a string (as with kmm_malloc) to hold the output, instead of
+ *   putting the output in a buffer you allocate in advance.  The ptr
+ *   argument should be the address of a char * object, and a successful
+ *   call to vasprintf stores a pointer to the newly allocated string at that
+ *   location.
+ *
+ * Returned Value:
+ *   The returned value is the number of characters allocated for the buffer,
+ *   or less than zero if an error occurred. Usually this means that the
+ *   buffer could not be allocated.
+ *
+ ****************************************************************************/
+
+int nx_vasprintf(FAR char **ptr, FAR const IPTR char *fmt, va_list ap)
+{
+  struct lib_outstream_s nulloutstream;
+  struct lib_memoutstream_s memoutstream;
+
+  /* On some architectures, va_list is really a pointer to a structure on
+   * the stack. And the va_arg builtin will modify that instance of va_list.
+   * Since vasprintf traverse the parameters in the va_list twice, the
+   * va_list will be altered in this first cases and the second usage will
+   * fail. This is a known issue with x86_64.
+   */
+
+#ifdef va_copy
+  va_list ap2;
+#endif
+  FAR char *buf;
+  int nbytes;
+
+  DEBUGASSERT(ptr && fmt);
+
+#ifdef va_copy
+  va_copy(ap2, ap);
+#endif
+
+  /* First, use a nullstream to get the size of the buffer.  The number
+   * of bytes returned may or may not include the null terminator.
+   */
+
+  lib_nulloutstream(&nulloutstream);
+  lib_vsprintf(&nulloutstream, fmt, ap);
+
+  /* Then allocate a buffer to hold that number of characters, adding one
+   * for the null terminator.
+   */
+
+  buf = kmm_malloc(nulloutstream.nput + 1);
+  if (!buf)

Review Comment:
   Optional
   ```suggestion
     if (buf == NULL)
   ```



##########
include/string.h:
##########
@@ -110,6 +110,14 @@ FAR void  *memmem(FAR const void *haystack, size_t 
haystacklen,
 void explicit_bzero(FAR void *s, size_t n);
 int timingsafe_bcmp(FAR const void *b1, FAR const void *b2, size_t n);
 
+#ifdef __KERNEL__
+#define strdup(s)       nx_strdup(s)
+#define strndup(s,sz)   nx_strndup(s,sz)

Review Comment:
   ```suggestion
   #  define strdup(s)     nx_strdup(s)
   #  define strndup(s,sz) nx_strndup(s,sz)
   ```



##########
libs/libc/string/lib_strndup.c:
##########
@@ -34,6 +34,47 @@
  * Public Functions
  ****************************************************************************/
 
+/****************************************************************************
+ * Name: nx_strndup
+ *
+ * Description:
+ *   The strndup() function is equivalent to the strdup() function,
+ *   duplicating the provided 's' in a new block of memory allocated as
+ *   if by using kmm_malloc(), with the exception being that strndup() copies
+ *   at most 'size' plus one bytes into the newly allocated memory,
+ *   terminating the new string with a NUL character. If the length of 's'
+ *   is larger than 'size', only 'size' bytes will be duplicated. If
+ *   'size' is larger than the length of 's', all bytes in s will be
+ *   copied into the new memory buffer, including the terminating NUL
+ *   character. The newly created string will always be properly
+ *   terminated.
+ *
+ ****************************************************************************/
+
+FAR char *nx_strndup(FAR const char *s, size_t size)
+{
+  FAR char *news = NULL;
+
+  /* Get the size of the new string (limited to size) */
+
+  size_t allocsize = strnlen(s, size);
+
+  /* Allocate the new string, adding 1 for the NUL terminator */
+
+  news = (FAR char *)kmm_malloc(allocsize + 1);
+  if (news)

Review Comment:
   Optional
   ```suggestion
     if (news != NULL)
   ```



##########
fs/fs_heap.h:
##########
@@ -25,6 +25,8 @@
  * Included Files
  ****************************************************************************/
 
+#include <stdio.h>
+#include <string.h>

Review Comment:
   Does not really brings impact, but would be good to include this after
   ```
   #include <nuttx/config.h>
   #include <nuttx/compiler.h>
   ```



##########
libs/libc/stdio/lib_vasprintf.c:
##########
@@ -34,6 +34,101 @@
  * Public Functions
  ****************************************************************************/
 
+/****************************************************************************
+ * Name: nx_vasprintf
+ *
+ * Description:
+ *   This function is similar to vsprintf, except that it dynamically
+ *   allocates a string (as with kmm_malloc) to hold the output, instead of
+ *   putting the output in a buffer you allocate in advance.  The ptr
+ *   argument should be the address of a char * object, and a successful
+ *   call to vasprintf stores a pointer to the newly allocated string at that
+ *   location.
+ *
+ * Returned Value:
+ *   The returned value is the number of characters allocated for the buffer,
+ *   or less than zero if an error occurred. Usually this means that the
+ *   buffer could not be allocated.
+ *
+ ****************************************************************************/
+
+int nx_vasprintf(FAR char **ptr, FAR const IPTR char *fmt, va_list ap)
+{
+  struct lib_outstream_s nulloutstream;
+  struct lib_memoutstream_s memoutstream;
+
+  /* On some architectures, va_list is really a pointer to a structure on
+   * the stack. And the va_arg builtin will modify that instance of va_list.
+   * Since vasprintf traverse the parameters in the va_list twice, the
+   * va_list will be altered in this first cases and the second usage will
+   * fail. This is a known issue with x86_64.
+   */
+
+#ifdef va_copy
+  va_list ap2;
+#endif
+  FAR char *buf;
+  int nbytes;
+
+  DEBUGASSERT(ptr && fmt);

Review Comment:
   Optional
   ```suggestion
     DEBUGASSERT(ptr != NULL && fmt != NULL);
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to