www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Catching std.conv.ConvException in readf causes a buffer overrun

reply Shigeki Karita <shigekikarita gmail.com> writes:
import std.stdio;
import std.conv;

void main() {
     int i;
     try {
         readf("%d\n", &i); // "a"
     } catch (ConvException e) {
         auto s = readln();
         writeln(s); // "aa", "aaa" or SEGV ???
     }
}

https://wandbox.org/permlink/NMYNjpOgQtfUprBQ

I just want to retry reading if input was invalid. How can I do 
it?
Jun 26 2018
parent Shigeki Karita <shigekikarita gmail.com> writes:
On Wednesday, 27 June 2018 at 02:39:49 UTC, Shigeki Karita wrote:
 import std.stdio;
 import std.conv;

 void main() {
     int i;
     try {
         readf("%d\n", &i); // "a"
     } catch (ConvException e) {
         auto s = readln();
         writeln(s); // "aa", "aaa" or SEGV ???
     }
 }

 https://wandbox.org/permlink/NMYNjpOgQtfUprBQ

 I just want to retry reading if input was invalid. How can I do 
 it?
I found that formattedRead!"%d\n"(readln(), i) can avoid the buffer overrun why readf cannot do this? --- import std.stdio; import std.format; import std.stdio; import std.format; void main() { int i; try { formattedRead!"%d\n"(readln(), i); } catch (Exception e) { // writeln(e); auto s = readln(); writeln(s); // "" } }
Jun 26 2018