2

Bearing in mind the answers given to a question about a safer formatting library for C, I'm wondering whether there is a safe C formatting library?

What I mean is:

  • there's no possibility to mismatch the format string from the arguments
  • there's no possibility to crash by passing the wrong type
  • there're no platform-dependent aspects

Please don't answer about the Microsoft Safe String Library, or libraries that are less unsafe but still not totally safe, as I'm aware of these, and they don't satisfy the requirements for total safety.

Thanks in advance

4

3 に答える 3

5

引数からフォーマット文字列が一致しない可能性はありません

フォーマット文字列が必要な場合は、特別なコンパイラ サポートがなければ基本的にできません。そうは言っても、フォーマット文字列を忘れれば、C で安全なフォーマット ライブラリを使用できます。私は何も知りませんが、それらが存在しても驚かないでしょう。

次のようなインターフェースを持つことができます。

typedef ... FORMATTER;

FORMATTER create_formatter();
int fmt_add_string_default(FORMATTER f, const char *s);
int fmt_add_string(FORMATTER f, const char *s, int maxlength, const char fill, enum fmt_alignment align);
...
int fmt_add_decimal_default(FORMATTER f, int d);
... // you get the idea
int fmt_write_result(FORMATTER f, char *out, int out_length);
void destroy_formatter(FORMATTER f);

少し冗長であれば、このようなものは完全に安全です。

于 2009-05-09T23:56:19.100 に答える