https://gcc.gnu.org/bugzilla/show_bug.cgi?id=121814

--- Comment #5 from liwei <liweifriends at gmail dot com> ---
Comment on attachment 62318
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=62318
The file that can re-pro the issue

>#include <algorithm>
>#include <iostream>
>#include <string>
>#include <stdexcept>
>
>void put(std::string& __s, const void* __vv)
>{
>    using _UIntPtrType = std::conditional_t<(sizeof(const void*) <= 
> sizeof(unsigned long)),
>                                            unsigned long,
>                                            unsigned long long>;
>
>    auto __v = reinterpret_cast<_UIntPtrType>(__vv);
>
>    const static char ss_out_atoms[] = {
>        '0', '1', '2', '3',
>        '4', '5', '6', '7',
>        '8', '9', 'a', 'b',
>        'c', 'd', 'e', 'f'
>    };
>
>    // Long enough to hold hex, dec, and octal representations.
>    constexpr auto __ilen = 5 * sizeof(_UIntPtrType);
>    char cs_vec[__ilen];
>    char* __cs = cs_vec;
>
>    _UIntPtrType __u = __v;
>
>    auto __buf = __cs + __ilen;
>    do
>    {
>         *--__buf = ss_out_atoms[(__u & 0xf)];
>        __u >>= 4;
>    } while (__u != 0);
>        
>    auto __len =  __cs + __ilen - __buf;
>
>    __cs += __ilen - __len;
>    
>    *--__cs = 'x';
>    *--__cs = '0';
>    __len += 2;
>    
>    for (auto c = __cs; c < __cs + __len; ++c)
>        __s += *c;
>}
>
>template <typename TIt>
>void get(TIt __beg, const TIt __end, void*& __v)
>{
>    using _UIntPtrType = std::conditional_t<(sizeof(const void*) <= 
> sizeof(unsigned long)),
>                                            unsigned long,
>                                            unsigned long long>;
>    _UIntPtrType __ul;
>
>    const static char s_in_atoms[] = {
>        '0', '1', '2', '3',
>        '4', '5', '6', '7',
>        '8', '9', 'a', 'b',
>        'c', 'd', 'e', 'f'
>    };
>
>    ++__beg;  // '0'
>    ++__beg;  // 'x'
>    char __c = *__beg;
>
>    // Extract.
>    _UIntPtrType __result = 0;
>    int __digit = 0;
>    const char* __lit_zero = s_in_atoms;
>
>    while (true)
>    {
>        const char* __q = std::find(__lit_zero, __lit_zero + 16, __c);
>        if (__q == __lit_zero + 16) break;
>    
>        __digit = __q - __lit_zero;
>        if (__digit > 15) __digit -= 6;
>        else
>        {
>            __result *= 16;
>            __result += __digit;
>        }
>        
>        if (++__beg != __end)
>            __c = *__beg;
>        else break;
>    }
>
>    __ul = __result;
>
>    __v = reinterpret_cast<void*>(__ul);
>}
>
>int main()
>{
>    // test void pointers
>    int i = 55;
>    int j = 3;
>    int* po = &i;
>    int* pi = &j;
>
>    std::string ss_01;
>    put(ss_01, po);
>
>    void* tmp = nullptr;
>    get(ss_01.begin(), ss_01.end(), tmp);
>    pi = reinterpret_cast<int*>(tmp);
>
>//    std::cout << pi << ' ' << po << std::endl;
>    if (pi != po) throw std::runtime_error("check fail");
>}

Reply via email to