June 27, 2018 Catching std.conv.ConvException in readf causes a buffer overrun | ||||
---|---|---|---|---|
| ||||
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? |
June 27, 2018 Re: Catching std.conv.ConvException in readf causes a buffer overrun | ||||
---|---|---|---|---|
| ||||
Posted in reply to Shigeki Karita | 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); // ""
}
}
|
Copyright © 1999-2021 by the D Language Foundation