Bug 18256 – Using std.range.put to put a character into a dchar[] segfaults

Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2018-01-17T16:08:11Z
Last change time
2018-03-22T18:49:53Z
Assigned to
No Owner
Creator
Jack Stouffer
See also
https://issues.dlang.org/show_bug.cgi?id=14998

Comments

Comment #0 by jack — 2018-01-17T16:08:11Z
import std.stdio; import std.range; void main(string[] args) { dchar[] a = cast(dchar[]) "aaa"d; // all three of these segfault put(a, cast(dchar) 'b'); put(a, cast(wchar) 'b'); put(a, cast(char) 'b'); }
Comment #1 by john.loughran.colvin — 2018-03-22T18:30:23Z
You're writing to a string literal, which isn't allowed. change to dchar[] a = "aaa"d.dup; and it works fine.
Comment #2 by jack — 2018-03-22T18:32:20Z
(In reply to John Colvin from comment #1) > You're writing to a string literal, which isn't allowed. > > change to > > dchar[] a = "aaa"d.dup; > > and it works fine. I'm aware it's not allowed. The operative term was segfault, as opposed to giving an error.
Comment #3 by john.loughran.colvin — 2018-03-22T18:49:53Z
This is nothing to do with `put`. As far as `put` is concerned it's been given a mutable buffer. A segfault is a good outcome when you try to write to a string literal. Do you mean a compile-time error? If so: I suggest opening an enhancement request for something like "Disallow obvious cases of writing to string literals" or similar, but I doubt you'll get much traction with it as it's only practically possible to do in trivial cases and only occurs when someone has already broken the rules by casting away immutable. If you mean a runtime error: That's what the segfault is for. There's no need to duplicate the work that the CPU does (with all the performance implications that would have). I'm gonna close this again, re-open if you're really motivated but I think a new issue would be better, without the confusion of `put`.