Skip to content

[BUG] Multiple syslog channels issue #19173

@michallenc

Description

@michallenc

Description / Steps to reproduce the issue

Issue reported by Martin Krasula, forwarding to Github.

Description

When using multiple channels for syslog output (for example, combining the NSH console, USB CDCACM, and/or logging to
a file stored in NOR flash memory), an infinite loop (cyclical syslog output) can occur.

This issue happens because the functions syslograwstream_addstring and syslograwstream_addchar do not check and handle the return value of syslograwstream_flush.

The syslograwstream_flush function can return a negative errno propagated from syslog_write -> syslog_write_foreach -> write.
This typically happens if a write operation to the syslog file fails, or when the USB CDCACM interface for syslog is disconnected.

Affected files

  • libs/libc/stream/lib_syslograwstream.c
  • drivers/syslog/syslog_write.c

Possible Solution

The solution would be to properly handle the return value from syslograwstream_flush to prevent the infinite loop when a errno occurs.
Please see the attached patch for more details.

diff --git a/libs/libc/stream/lib_syslograwstream.c b/libs/libc/stream/lib_syslograwstream.c
index 76b149f1d3..61154deb01 100644
--- a/libs/libc/stream/lib_syslograwstream.c
+++ b/libs/libc/stream/lib_syslograwstream.c
@@ -123,7 +123,13 @@ static void syslograwstream_addchar(FAR struct lib_syslograwstream_s *stream,
 
       /* Yes.. then flush the buffer */
 
-      syslograwstream_flush(&stream->common);
+      int ret = syslograwstream_flush(&stream->common);
+      if (ret < 0)
+      {
+        fprintf(stderr, "syslograwstream_flush: %d char: %c\n", ret, ch);
+        stream->offset = 0;
+        stream->common.nput = 0;
+      }
     }
 }
 
@@ -136,7 +142,6 @@ syslograwstream_addstring(FAR struct lib_syslograwstream_s *stream,
                           FAR const char *buff, size_t len)
 {
   ssize_t ret = 0;
-
   do
     {
       size_t remain = CONFIG_SYSLOG_BUFSIZE - stream->offset;
@@ -154,8 +159,15 @@ syslograwstream_addstring(FAR struct lib_syslograwstream_s *stream,
           stream->offset = CONFIG_SYSLOG_BUFSIZE + 1;
 
           /* Yes.. then flush the buffer */
+          int ret_flush = syslograwstream_flush(&stream->common);
+          if (ret_flush < 0)
+          {
+             fprintf(stderr, "syslograwstream_flush: %d\n", ret_flush);
+             stream->offset = 0;
+             stream->common.nput = 0;
+             break;
+          }
 
-          syslograwstream_flush(&stream->common);
         }
     }
   while (ret < len);

On which OS does this issue occur?

[OS: Other], [OS: Linux], [OS: Windows], [OS: Mac], [OS: BSD]

What is the version of your OS?

NuttX

NuttX Version

master

Issue Architecture

[Arch: all]

Issue Area

[Area: OS Components]

Host information

No response

Verification

  • I have verified before submitting the report.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Arch: allIssues that apply to all architecturesArea: OS ComponentsOS Components issuesOS: BSDIssues related to *BSD OSes (building system, etc)OS: LinuxIssues related to Linux (building system, etc)OS: MacIssues related to MacOS (building system, etc)OS: OtherIssues related to other OS (not Linux, Mac or Win)OS: WindowsIssues related to Windows (building system, etc)Type: BugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions