GraphicsLessonInGforth
GraphicsLessonInGforth copied to clipboard
Terminate-String word is unsafe
ANS Forth forbids modification of strings returned by the S"
word (a.k.a. s-quote
). (See also this discussion, ctrl-f for shall not alter.) This means that the Terminate-String
word is non-compliant. More specifically, its intended use, in combination with S"
, is non-compliant.
I think the standard-compliant solution is to use the S\"
word instead, where the \z
sequence generates a null character, like '\0'
in C, suitable for termination.
See also this StackOverflow thread, although he uses \0
rather than \z
, which doesn't seem right.
From what I can see, ANS Forth offers no similar words for producing mutable strings. Perhaps a copy-based approach could also be made to work.
@MaxBarraclough Thanks for your feedback. I'm a Forth beginner and my skills are less good as yours. In which file/line the returned string is modified?
I'm not very experienced with Forth either, I was just lucky enough to spot that part of the spec and notice the issue.
My thinking is that if you use S\"
with the \z
escape-sequence, you should be able to get null-terminated strings directly from the literals without having to ever modify them. Perhaps the Terminate-String
word could then be retired completely.
Here are two uses of s"
and Terminate-String
that could be replaced by s\"
:
Thanks for the report ! Since I'm a beginner and mainly use these codes as inspiration and goals for my own forth, I prefer not touching the code and let a professional offering a pull request.