digitalmars.D.learn - Catching std.conv.ConvException in readf causes a buffer overrun
- Shigeki Karita (14/14) Jun 26 2018 import std.stdio;
- Shigeki Karita (18/32) Jun 26 2018 I found that formattedRead!"%d\n"(readln(), i) can avoid the
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
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