3 ms·
This is a link to https://sourceforge.net/projects/safeclib/ https://sourceforge.net/projects/safeclib/ , an implementation of ISO/IEC TR 24731-1. In brief, TR2
by geofft 10y ago
This is a link to https://sourceforge.net/projects/safeclib/ https://sourceforge.net/projects/safeclib/ , an implementation of ISO/IEC TR 24731-1. In brief, TR24731-1 provides:
- a type rsize_t, typedef'd to size_t, but by convention limited to a lower value RSIZE_MAX that is ideally no larger than SIZE_MAX/2, thereby preventing some classes of arithmetic overflows (adding two valid rsize_ts never overflows, but the next function using it will complain at runtime if it exceeds RSIZE_MAX)
- a large number of functions ending in _s, like memcpy_s and tmpnam_s, which use rsize_t as their size type, take lengths with every buffer, and return an errno instead of a direct value
Wikipedia says that TR24731-1 "has met severe criticism with some praise.... Despite this, TR 24731-1 has been implemented into Microsoft's C standard library and its compiler issues warnings when using old 'insecure' functions." glibc 2.25, released last month, has provisional support for it.
Wikipedia links to this Austin Group report criticizing TR24731: http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1106.txt http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1106.txt
The approach of simply providing a length argument to a number of string functions may cause as many problems as it solves. It provides no assurance that the programmer provide a sensible value, and may end up obfuscating otherwise clear and safe usage of the original function. Functions that allocate memory (using malloc()) would provide safer, clearer, and more robust interfaces. E.g. strdup() instead of strcpy().
and this Stack Overflow thread: https://stackoverflow.com/questions/372980/do-you-use-the-tr-24731-safe-functions https://stackoverflow.com/questions/372980/do-you-use-the-tr...
On a side note, especially now that it's a decade or so after TR24731-1 was released, there are many good languages (some compiled, some interpreted, only one of which rhymes with "overdiscussed") that are suitable to a lot of the things you'd want to use C for a decade ago, and solve the problem in a straightforward way that resolves the objection above: they provide either buffer types that remember their own capacity, string objects capable of allocating as necessary, or both. There are also C libraries that do the same (bstring and GTK+ come to mind), if you're inclined to continue writing C.
- Gibbon1 10y ago> (using malloc()) would provide safer, clearer, and more robust interfaces. E.g. strdup() instead of strcpy(). malloc() ... Lutz
- rurban 10y agoI forked it at https://github.com/rurban/safeclib https://github.com/rurban/safeclib Besides the mentioned critism there are several outstanding issues I'll fix: add sprintf_s, fix strljustify_s from the 2 unmerged merge requests. fix memset_s API to be C11 compatible.
- rurban 10y agoDone and merge request for 2.0 sent upstream.