Przeglądaj źródła

rpc/common: Fix potential panic on read_from_stream

x 2 miesięcy temu
rodzic
commit
51b110af88
1 zmienionych plików z 11 dodań i 2 usunięć
  1. 11 2
      src/rpc/common.rs

+ 11 - 2
src/rpc/common.rs

@@ -194,11 +194,12 @@ pub(super) async fn read_from_stream(
             Ok(_) => {
                 // When we reach '\n', pop a possible '\r' from the buffer and bail.
                 if tmpbuf[0] == b'\n' {
-                    if buf[total_read - 1] == b'\r' {
+                    if total_read > 0 && buf[total_read - 1] == b'\r' {
                         buf.pop();
                         total_read -= 1;
                     }
-                    break
+                    buf.truncate(total_read);
+                    return Ok(total_read)
                 }
 
                 // Copy the read byte to the destination buffer.
@@ -210,6 +211,14 @@ pub(super) async fn read_from_stream(
         }
     }
 
+    // Loop exited without finding \n.
+    if total_read >= MAX_BUF_SIZE {
+        return Err(io::Error::new(
+            io::ErrorKind::InvalidData,
+            "line exceeded maximum buf size without terminator",
+        ))
+    }
+
     // Truncate buffer to actual data size
     buf.truncate(total_read);
     Ok(total_read)