huojianchaoyy commented on code in PR #21787:
URL: https://github.com/apache/nuttx/pull/21787#discussion_r4559009495


##########
drivers/syslog/syslog_write.c:
##########
@@ -187,10 +187,14 @@ ssize_t syslog_write_foreach(int priority, FAR const char 
*buffer,
                       continue;
                     }
 
-                  ret = write(channel, buffer + nwritten, head - nwritten);
-                  if (ret <= 0)
+                  if (head > nwritten)
                     {
-                      continue;
+                      ret = write(channel, buffer + nwritten,

Review Comment:
        It never writes the same data twice. The second `write()` from the same 
                                                               
        `nwritten` happens only after the first one returned <= 0, which means 
the                                                             
        channel stored nothing (0 = buffer full, negative = error) — those 
bytes                                                               
        never reached the channel, so re-submitting them at the next '\n' or in 
the                                                            
        tail write is a retry, not a duplicate.                                 
                                                               
                                                                                
                                                               
        The case that could really duplicate is the opposite one: when the 
CR/LF                                                               
        write fails, the text before it is already stored. That is why 
`nwritten`                                                              
        is advanced to `head` on that path (`nwritten = ret > 0 ? head + 1 : 
head`),                                                           
        so only the pending '\n' is left to the tail write — as the comment 
above                                                              
        the CR/LF write says, rewinding would duplicate the bytes already 
written.                                                             
                                                                                
                                                               
        This relies on `sc_write()` being all-or-nothing (full count, 0, or a   
                                                               
        negated errno), which is what the in-tree channels implement            
                                                               
        (`syslog_dev_write()` returns `buflen` or a negated errno; 
`ramlog_write()`                                                            
        and `syslog_rpmsg_write()` always return the full count). For 
reference,                                                               
        master did duplicate the text when the CR/LF write failed with an 
error:                                                               
        it continued without updating `nwritten`, so the next '\n' re-sent the  
                                                               
        already-output text — that is exactly what this change fixes. 



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

Reply via email to