LLVM Bugzilla is read-only and represents the historical archive of all LLVM issues filled before November 26, 2021. Use github to submit LLVM bugs

Bug 42166 - to_chars can puts leading zeros on numbers
Summary: to_chars can puts leading zeros on numbers
Status: RESOLVED FIXED
Alias: None
Product: libc++
Classification: Unclassified
Component: All Bugs (show other bugs)
Version: unspecified
Hardware: All All
: P enhancement
Assignee: Unassigned Clang Bugs
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2019-06-06 08:47 PDT by Marshall Clow (home)
Modified: 2019-06-10 14:14 PDT (History)
5 users (show)

See Also:
Fixed By Commit(s):


Attachments
Runtime tests for std::to_chars<integral type> (27.61 KB, text/plain)
2019-06-06 09:21 PDT, Jonathan Wakely
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Marshall Clow (home) 2019-06-06 08:47:47 PDT
Given the following code:
------
#include <charconv>
#include <iostream>
#include <cassert>

template <typename T>
void test()
{
	char buf[100];
	auto res = std::to_chars(buf, buf + 100, (T)0xffffffff);
	assert(res.ec == std::errc());
	*res.ptr = '\0';
	std::cout << (const char *) buf << std::endl;
}

int main ()
{
	test<int>();
	test<long>();
	test<int64_t>();
	
	test<unsigned int>();
	test<unsigned long>();
	test<uint64_t>();
}
-----

I expect this to print:
-1
4294967295
4294967295
4294967295
4294967295
4294967295


but instead it prints:
-1
0004294967295
0004294967295
4294967295
0004294967295
0004294967295
Comment 1 Jonathan Wakely 2019-06-06 09:21:37 PDT
Created attachment 22076 [details]
Runtime tests for std::to_chars<integral type>

These are my tests for to_chars for integral types. These were written for libstdc++ and contributed to GCC, but as the sole author I'm free to relicense them for distribution in libc++ as well. Which I'm doing now.

There are currently several failures when using libc++. The first few seem to be the issue described here. There are also some failures in base 2 output, but I didn't analyze them.
Comment 2 Zhihao Yuan 2019-06-06 10:38:15 PDT
(In reply to Jonathan Wakely from comment #1)
> 
> These are my tests for to_chars for integral types. These were written for
> libstdc++ and contributed to GCC, but as the sole author I'm free to
> relicense them for distribution in libc++ as well. Which I'm doing now.
> 

Thanks for your tests.

> There are currently several failures when using libc++. The first few seem
> to be the issue described here.

Yes, I walked through the original code (branchlut3) contributed by RapidJSON author in debugger and saw the same issue.  The issue doesn't present in branchlut2.  I'm going to contacting him to look at it.

> There are also some failures in base 2
> output, but I didn't analyze them.

I looked into them, and I think your test cases are asking for something not required by the standard.  The standard does not guarantee that we don't modify the space after r.ptr in the buffer.  My code may write to anywhere in the buffer provided https://github.com/llvm-mirror/libcxx/blob/master/include/charconv#L377 then move the content to the front if needed.
Comment 3 Marshall Clow (home) 2019-06-06 11:40:00 PDT
Vlad has proposed a fix here: https://reviews.llvm.org/D59178
I am looking into it as well.
Comment 4 Jonathan Wakely 2019-06-06 12:01:09 PDT
Good catch, I should mark those checks as  GNU-specific in our copy, and you won't want to keep that part in the libc++ tests.
Comment 5 Ivan Afanasyev 2019-06-08 07:26:22 PDT
Bugfix proposal:
https://reviews.llvm.org/D63047


I will be very grateful for a help with review.
Comment 6 Marshall Clow (home) 2019-06-10 14:14:03 PDT
Fixed in revision 362967.