PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, October 30, 2022

[FIXED] Why does reading a line of input from /dev/zero make my computer unresponsive?

 October 30, 2022     eof, readline, rust     No comments   

Issue

When building this code:

use std::io;

fn main() {
    let mut guess = String::new();
    let res = io::stdin()
        .read_line(&mut guess)
        .expect("Failed to read line");
}

and then executing:

cargo run < /dev/zero

My computer (under Ubuntu 20) becomes unresponsive.

I have read the manual page of the readline function and this behavior doesn't seem normal.

What am I missing?


Solution

/dev/zero is a never-ending stream of NUL bytes. read_line stops when it reads a newline symbol. So, this function will never return and will try to infinitely expand the buffer. The reason your computer becomes unresponsive is likely because it starts swapping heavily, in order to accommodate the ever-growing buffer.

read_line() is definitely not what you want. Try reading a fixed amount of bytes instead?



Answered By - justinas
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing