]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
ptyfwd: fix infinite loop
authorYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 25 Dec 2024 05:10:11 +0000 (14:10 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 25 Dec 2024 08:17:22 +0000 (17:17 +0900)
This makes we exit from the loop in do_shovel() when
PTYForward.out_buffer_write_len is zero but PTYForward.out_buffer_full
is non-zero.

Fixes a bug introduced by 5e6a48bf99d2adb3c9d22414197a593f2aa8a121.
Fixes #35746.

src/shared/ptyfwd.c

index f0b45dd918d1b929be9d1de70ab7c97c4e8b8b41..e66c4edf5812cb1dc3c0d42905a42943002e3b89 100644 (file)
@@ -619,10 +619,8 @@ static int do_shovel(PTYForward *f) {
                 f->out_buffer_size = MALLOC_SIZEOF_SAFE(p);
         }
 
-        while ((f->stdin_readable && f->in_buffer_full <= 0) ||
-               (f->master_writable && f->in_buffer_full > 0) ||
-               (f->master_readable && f->out_buffer_full <= 0) ||
-               (f->stdout_writable && f->out_buffer_full > 0)) {
+        for (;;) {
+                bool did_something = false;
 
                 if (f->stdin_readable && f->in_buffer_full < LINE_MAX) {
 
@@ -652,6 +650,8 @@ static int do_shovel(PTYForward *f) {
 
                                 f->in_buffer_full += (size_t) k;
                         }
+
+                        did_something = true;
                 }
 
                 if (f->master_writable && f->in_buffer_full > 0) {
@@ -673,6 +673,8 @@ static int do_shovel(PTYForward *f) {
                                 memmove(f->in_buffer, f->in_buffer + k, f->in_buffer_full - k);
                                 f->in_buffer_full -= k;
                         }
+
+                        did_something = true;
                 }
 
                 if (f->master_readable && f->out_buffer_full < MIN(f->out_buffer_size, (size_t) LINE_MAX)) {
@@ -702,6 +704,8 @@ static int do_shovel(PTYForward *f) {
                                 if (r < 0)
                                         return log_error_errno(r, "Failed to scan for ANSI sequences: %m");
                         }
+
+                        did_something = true;
                 }
 
                 if (f->stdout_writable && f->out_buffer_write_len > 0) {
@@ -738,7 +742,12 @@ static int do_shovel(PTYForward *f) {
                                 f->out_buffer_full -= k;
                                 f->out_buffer_write_len -= k;
                         }
+
+                        did_something = true;
                 }
+
+                if (!did_something)
+                        break;
         }
 
         if (f->stdin_hangup || f->stdout_hangup || f->master_hangup) {