civodul pushed a commit to branch main
in repository shepherd.

commit d98d61a8a3f20de46d18ce4a8af05c93fab20b89
Author: Ludovic Courtès <[email protected]>
AuthorDate: Mon Mar 31 23:04:52 2025 +0200

    system-log: Work around ‘regexp-exec’ bug in non-Unicode capable locale.
    
    Fixes <https://issues.guix.gnu.org/77283>.
    
    * modules/shepherd/service/system-log.scm (%system-log-message-rx):
    Remove suffix from regexp.
    (parse-system-log-message): Call ‘match:suffix’.
    * tests/services/system-log-internal.scm (call-with-c-locale): New
    procedure.
    (with-c-locale): New macro.
    ("read-system-log-message, with Unicode and C locale"): New test.
    * NEWS: Update.
    
    Reported-by: MSavoritias <[email protected]>
---
 NEWS                                    |  8 ++++++++
 modules/shepherd/service/system-log.scm | 10 ++++++----
 tests/services/system-log-internal.scm  | 27 ++++++++++++++++++++++++++-
 3 files changed, 40 insertions(+), 5 deletions(-)

diff --git a/NEWS b/NEWS
index e6a7cfe..b6663e3 100644
--- a/NEWS
+++ b/NEWS
@@ -61,6 +61,14 @@ would be incorrectly calculated when the event would fall 
between 02:00am and
 03:00am, leading the timer to trigger many times in a row, unless it had
 #:wait-for-termination? #true.  This is now fixed; next year will be better!
 
+** System log no longer crashes on some Unicode input
+   (<https://issues.guix.gnu.org/77283>)
+
+Due to a bug in Guile’s (ice-9 regex) module, the ‘system-log’ service could
+crash on certain inputs containing non-ASCII Unicode characters, when
+‘shepherd’ is running in a non-Unicode capable locale (which is usually the
+case for PID 1).  This is now fixed by sidestepping the regexp bug entirely.
+
 * Changes in 1.0.3
 
 ** ‘spawn-command’ now honors #:log-file
diff --git a/modules/shepherd/service/system-log.scm 
b/modules/shepherd/service/system-log.scm
index bf9ca69..fb31c12 100644
--- a/modules/shepherd/service/system-log.scm
+++ b/modules/shepherd/service/system-log.scm
@@ -122,10 +122,12 @@
           %system-log-priority-mask))
 
 (define %system-log-message-rx
-  ;; Regexp matching system log messages.  Example:
+  ;; Regexp matching the prefix of system log messages.  Example:
   ;;   <29>Jun 22 16:41:30 wpa_supplicant[303]: whatever
-  (make-regexp "<([0-9]+)> ?([[:alpha:]]{3} +[0-9]+ [0-9]+:[0-9]+:[0-9]+ )?\
-(.*)"))
+  ;; Note: Do not match the actual message (the suffix) because it might
+  ;; contain Unicode code points and 'regexp-exec' might be buggy in that
+  ;; case: <https://bugs.gnu.org/77392>.
+  (make-regexp "<([0-9]+)> ?([[:alpha:]]{3} +[0-9]+ [0-9]+:[0-9]+:[0-9]+ )?"))
 
 (define %default-priority (system-log-priority notice))
 (define %default-facility (system-log-facility user))
@@ -140,7 +142,7 @@ representing it."
     (m
      (let* ((facility+priority (string->number (match:substring m 1))))
        (system-log-message facility+priority
-                           (match:substring m 3) sender)))))
+                           (match:suffix m) sender)))))
 
 (define* (read-system-log-message port #:optional sender)
   "Read a system log message from @var{port}.  Return the end-of-file object
diff --git a/tests/services/system-log-internal.scm 
b/tests/services/system-log-internal.scm
index 87e36c1..d4280c1 100644
--- a/tests/services/system-log-internal.scm
+++ b/tests/services/system-log-internal.scm
@@ -1,5 +1,5 @@
 ;; GNU Shepherd --- Test the system log service.
-;; Copyright © 2024 Ludovic Courtès <[email protected]>
+;; Copyright © 2024-2025 Ludovic Courtès <[email protected]>
 ;;
 ;; This file is part of the GNU Shepherd.
 ;;
@@ -20,6 +20,17 @@
   #:use-module (shepherd service system-log)
   #:use-module (srfi srfi-64))
 
+(define (call-with-c-locale thunk)
+  (let ((previous #f))
+    (dynamic-wind
+      (lambda () (set! previous (setlocale LC_ALL "C")))
+      thunk
+      (lambda () (setlocale LC_ALL previous)))))
+
+(define-syntax-rule (with-c-locale exp)
+  (call-with-c-locale (lambda () exp)))
+
+
 (test-begin "system-log-internal")
 
 (test-equal "read-system-log-message, with PID"
@@ -46,6 +57,20 @@ sudo: ludo : TTY=pts/0 ; PWD=/home/ludo ; USER=root ; 
COMMAND=xyz"
               (system-log-message-priority message)
               (system-log-message-content message))))))
 
+(test-equal "read-system-log-message, with Unicode and C locale"
+  (list (system-log-facility daemon)
+        (system-log-priority debug)
+        "prosody[198]: Ϫ")
+  ;; This used to trigger a bug whereby 'regexp-exec' (in Guile 3.0.9) would
+  ;; incorrectly compute match boundaries when running in a non-Unicode
+  ;; capable locale: <https://issues.guix.gnu.org/77283>.
+  (call-with-input-string "<31>Mar 26 16:41:31 prosody[198]: Ϫ"
+    (lambda (port)
+      (let ((message (with-c-locale (read-system-log-message port))))
+        (list (system-log-message-facility message)
+              (system-log-message-priority message)
+              (system-log-message-content message))))))
+
 (test-equal "read-system-log-message, raw"
   (list (system-log-facility user)
         (system-log-priority notice)

Reply via email to