Safer Alternatives to the C Standard Library

后端 未结 6 1499
广开言路
广开言路 2021-02-07 10:29

The C standard library is notoriously poor when it comes to I/O safety. Many functions have buffer overflows (gets, scanf), or can clobber memory if n

相关标签:
6条回答
  • 2021-02-07 11:02

    This isn't really answering your question about the safest libraries to use, but most functions that are vulnerable to buffer overflows that you mentioned have safer versions which take the buffer length as an argument to prevent the security holes that are opened up when the standard methods are used.

    Unless you have relaxed the level of warnings, you will usually get compiler warnings when you use the deprecated methods, suggesting you use the safer methods instead.

    0 讨论(0)
  • 2021-02-07 11:05

    I believe the Apache Portable Runtime (apr) library is safer than the standard C library. I use it, well, as part of an apache module, but also for independent processes.

    0 讨论(0)
  • 2021-02-07 11:15

    For Windows there is a 'safe' C/C++ library.

    0 讨论(0)
  • 2021-02-07 11:20

    Maybe the first question to ask is if your really need plain C? (maybe a language like .net or java is an option - then e.g. buffer overflows are not really a problem anymore)

    Another option is maybe to write parts of your project in C++ if other higher level languages are not an option. You can then have a C interface which encapsulates the C++ code if you really need C.

    Because if you add all the advanced functions the C++ standard library has build in - your C code would only be marginally faster most times (and contain a lot more bugs than an existing and tested framework).

    0 讨论(0)
  • 2021-02-07 11:23

    I use GLib library, it has many good standard and non standard functions.

    See https://developer.gnome.org/glib/stable/

    and maybe you fall in love... :)

    For example:

    https://developer.gnome.org/glib/stable/glib-String-Utility-Functions.html#g-strdup-printf

    explains that g_strdup_printf is:

    Similar to the standard C sprintf() function but safer, since it calculates the maximum space required and allocates memory to hold the result.

    0 讨论(0)
  • 2021-02-07 11:28

    You're always at liberty to implement any library you like and to use it - the hard part is making sure it is available on the platforms you need your software to work on. You can also use wrappers around the standard functions where appropriate.

    Whether it is really a good idea is somewhat debatable, but there is TR24731 published by the C standard committee - for a safer set of C functions. There's definitely some good stuff in there. See this question: Do you use the TR 24731 Safe Functions in your C code?, which includes links to the technical report.

    0 讨论(0)
提交回复
热议问题