pkarashchenko commented on code in PR #6666:
URL: https://github.com/apache/incubator-nuttx/pull/6666#discussion_r928753431


##########
libs/libc/wchar/lib_wcrtomb.c:
##########
@@ -57,18 +51,37 @@
 
 size_t wcrtomb(FAR char *s, wchar_t wc, FAR mbstate_t *ps)
 {
-  int retval = 0;
-  char buf[MB_LEN_MAX];
-
   if (s == NULL)
     {
-      retval = wctomb(buf, wc);
+      return 0;
+    }
+  else if ((unsigned)wc < 0x80)
+    {
+      *s = wc;
+      return 1;
+    }
+  else if ((unsigned)wc < 0x800)
+    {
+      *s++ = 0xc0 | (wc >> 6);
+      *s = 0x80 | (wc & 0x3f);
+      return 2;
+    }
+  else if ((unsigned)wc < 0xd800 || (unsigned)wc - 0xe000 < 0x2000)
+    {
+      *s++ = 0xe0 | (wc >> 12);
+      *s++ = 0x80 | ((wc >> 6) & 0x3f);
+      *s = 0x80 | (wc & 0x3f);
+      return 3;
     }
-  else
+  else if ((unsigned)wc - 0x10000 < 0x100000)

Review Comment:
   So here we assume that `int` is at lease 32bit? Maybe switching to fixed 
sized `uint32_t` or at least to `unsigned long` is better? Also logic can be 
reworked to avoid subtraction of unsigned: `else if ((unsigned)wc < 0x110000)`



##########
libs/libc/wchar/lib_wcsnrtombs.c:
##########
@@ -72,50 +67,57 @@
 size_t wcsnrtombs(FAR char *dst, FAR const wchar_t **src, size_t nwc,
                   size_t len, FAR mbstate_t *ps)
 {
-  size_t i;
+  FAR const wchar_t *ws = *src;
+  size_t cnt = 0;
 
   if (dst == NULL)
     {
-      for (i = 0; i < nwc; i++)
+      len = 0;
+    }
+
+  while (ws != NULL && nwc != 0)
+    {
+      char tmp[MB_LEN_MAX];
+      size_t l;
+
+      if (*ws == 0)
         {
-          wchar_t wc = (*src)[i];
+          ws = NULL;
+          break;
+        }
 
-          if (wc < 0 || wc > 0xff)
-            {
-              set_errno(EILSEQ);
-              return -1;
-            }
+      l = wcrtomb(len < MB_LEN_MAX ? tmp : dst, *ws, ps);
+      if ((ssize_t)l < 0)

Review Comment:
   Posting a comment here just to ensure that `0` is handled properly. It is 
hard to track `l == 0` path



-- 
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