anchao commented on code in PR #20853:
URL: https://github.com/apache/nuttx/pull/20853#discussion_r4336403374
##########
sched/task/task_setup.c:
##########
@@ -614,16 +635,23 @@ int nxtask_setup_stackargs(FAR struct tcb_s *tcb,
for (i = 0; i < argc; i++)
{
/* Save the pointer to the location in the string buffer and
- * copy the argument into the buffer. Increment str to skip
- * over the argument and its NUL terminator in the string
- * buffer.
+ * copy the argument into the buffer. Use cached lengths for
+ * the common short argv case. For larger argv lists, stpcpy()
+ * still avoids the separate strlen() before copying and
+ * returns the address of the terminating NUL.
*/
stackargv[i + 1] = str;
- nbytes = strlen(argv[i]) + 1;
- strlcpy(str, argv[i], strtablen);
- str += nbytes;
- strtablen -= nbytes;
+ if (i < NXTASK_ARG_LEN_CACHE_SIZE)
+ {
+ nbytes = arglen[i];
+ memcpy(str, argv[i], nbytes);
+ str += nbytes;
+ }
+ else
+ {
+ str = stpcpy(str, argv[i]) + 1;
Review Comment:
stpcpy() is already used for the uncached argv entries.
For the task name and cached argv entries, we already computed the length in
the sizing pass, so memcpy() can copy the known number of bytes directly.
stpcpy() would avoid a separate strlen(), but it still scans for NUL while
copying. The cache avoids that second scan for the common small-argv case.
So this patch uses memcpy() when the length is already known, and stpcpy()
only as the fallback when we intentionally do not cache more lengths to keep
stack usage small.
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]