Is append in APFloat broken?

Hi @llvm,

I stumbled over a strange behavior if a float containing a NaN is printed (e.g. in the clang rewriter). The local template method "append" in APFloat.cpp deduces the size from the char array, which for "NaN" is 4 (including the trailing zero). If APFloat::toString is called with a SmallString and then SmallString::str() is called, it returns "NaN\0". I guess that this is not intended. Maybe it can be fixed by the simple patch attached.

Best regards
Olaf Krzikalla

apfloat.patch (691 Bytes)

Thanks for the diagnosis.

Do you have a test case to go along with this?
The simpler fix seems to be to untemplate this code & have "append"
take a StringRef. We can just rely on the compiler to optimize away
the strlen in StringRef's (const char*) ctor as we do across the rest
of the codebase. That way this won't go awry if append is called with
a large buffer or a string without a nul terminator, etc.

- David

Do you have a test case to go along with this?

Unfortunately not. It just popped up as a result of a programming error made by me.

The simpler fix seems to be to untemplate this code& have "append"
take a StringRef. We can just rely on the compiler to optimize away
the strlen in StringRef's (const char*) ctor as we do across the rest
of the codebase. That way this won't go awry if append is called with
a large buffer or a string without a nul terminator, etc.

This is of course fine, too.

Best regards
Olaf

Committed as r160744 - thanks for the report!

- David