[OT] parsing with sscanf is accidentally quadratic due to strlen
Patrick Schluter
Patrick.Schluter at bbox.fr
Wed Mar 3 11:50:19 UTC 2021
On Wednesday, 3 March 2021 at 11:36:54 UTC, Kagamin wrote:
> On Wednesday, 3 March 2021 at 11:09:04 UTC, Patrick Schluter
> wrote:
>> Yes, sscanf() calls strlen(). I got bitten by it also some
>> years ago when I memory mapped some log files to parse
>
> I think you had another bug there? Memory mapped files are not
> null terminated with probability 1/4096.
No, that bug was tested for. If the file size was a multiple of
pagesize (it was on SPARC so 8192 not 4096) I added 1 byte to it
which forced mmap to add an empty page at the end => guaranteed 0
at the end.
If you're guaranteed to run on Linux you can force the adding of
a supplemental page just by passing a size bigger than the file
size to mmap. This was not possible on newer versions of Solaris.
This said, I had a library to handle these details, also by
replacing mmap by a malloc/read when the file is below a certain
size as mmap has a non negligible overhead.
More information about the Digitalmars-d
mailing list