This can be technically safe since you never change the memory pointed to by the CString value. Therefore, the values ββare really immutable, and the unsafePerformIO effect simply mimics a pure value.
However, the above is only done because you never release the memory, and just leak it every time you allocate a new line using newCString .
As soon as you fix a memory leak with free , you lose security: unsafePerformIO will read everything that is on the previous memory address. If you're lucky, this will crash the program. If you're out of luck, this will read the garbage data into your lines. Good luck in debugging.
I would highly recommend against this approach.
TL DR:
unsafePerformIO not safe, do not use it if you do not know what you are doing.- C-like pointers, such as
CString , should be used as little as possible and only for interaction with foreign languages.
source share