Catching std.conv.ConvException in readf causes a buffer overrun
Shigeki Karita
shigekikarita at gmail.com
Wed Jun 27 04:42:30 UTC 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); // ""
}
}
More information about the Digitalmars-d-learn
mailing list