|  | 
 
 发表于 2022-1-22 00:53:18
|
显示全部楼层 
| 复制代码$ cat main.c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
/*
=============
itoa
Convert integer to string
PARAMS:
- value     A 64-bit number to convert
- str       Destination buffer; should be 66 characters long for radix2, 24 - radix8, 22 - radix10, 18 - radix16.
- radix     Radix must be in range -36 .. 36. Negative values used for signed numbers.
=============
*/
#include <stdbool.h>
char* itoa (unsigned long long  value,  char str[],  int radix)
{
    char        buf [66];
    char*       dest = buf + sizeof(buf);
    //boolean     sign = false;
    bool        sign = false;
    if (value == 0) {
        memcpy (str, "0", 2);
        return str;
    }
    if (radix < 0) {
        radix = -radix;
        if ( (long long) value < 0) {
            value = -value;
            sign = true;
        }
    }
    *--dest = '\0';
    switch (radix)
    {
    case 16:
        while (value) {
            * --dest = '0' + (value & 0xF);
            if (*dest > '9') *dest += 'A' - '9' - 1;
            value >>= 4;
        }
        break;
    case 10:
        while (value) {
            *--dest = '0' + (value % 10);
            value /= 10;
        }
        break;
    case 8:
        while (value) {
            *--dest = '0' + (value & 7);
            value >>= 3;
        }
        break;
    case 2:
        while (value) {
            *--dest = '0' + (value & 1);
            value >>= 1;
        }
        break;
    default:            // The slow version, but universal
        while (value) {
            *--dest = '0' + (value % radix);
            if (*dest > '9') *dest += 'A' - '9' - 1;
            value /= radix;
        }
        break;
    }
    if (sign) *--dest = '-';
    memcpy (str, dest, buf +sizeof(buf) - dest);
    return str;
}
int main ()
{
        int i,n;
        char b[100];
        printf("请输入需要需要转换的数字:");
        scanf("%d",&i);
        printf("请输入最终显示位数:");
        scanf("%d",&n);
        //i + 2^n 转为2进制字符串
        itoa (i+(1<<n),b,2);
        //去掉最高位的1
        strcpy(b,b+1);
        printf("最终结果为:%s\n",b);
        return 0;
}
$ gcc-debug -o main main.c
$ ./main
请输入需要需要转换的数字:13
请输入最终显示位数:16
=================================================================
==610718==ERROR: AddressSanitizer: strcpy-param-overlap: memory ranges [0x7ffc5fe90030,0x7ffc5fe90041) and [0x7ffc5fe90031, 0x7ffc5fe90042) overlap
    #0 0x7f3976bf9b37 in __interceptor_strcpy /build/gcc/src/gcc/libsanitizer/asan/asan_interceptors.cpp:438
    #1 0x5596879c9ea9 in main /tmp/main.c:99
    #2 0x7f397608eb24 in __libc_start_main (/usr/lib/libc.so.6+0x27b24)
    #3 0x5596879c918d in _start (/tmp/main+0x218d)
Address 0x7ffc5fe90030 is located in stack of thread T0 at offset 80 in frame
    #0 0x5596879c9c4d in main /tmp/main.c:88
  This frame has 3 object(s):
    [48, 52) 'i' (line 89)
    [64, 68) 'n' (line 89)
    [80, 180) 'b' (line 90) <== Memory access at offset 80 is inside this variable
HINT: this may be a false positive if your program uses some custom stack unwind mechanism, swapcontext or vfork
      (longjmp and C++ exceptions *are* supported)
Address 0x7ffc5fe90031 is located in stack of thread T0 at offset 81 in frame
    #0 0x5596879c9c4d in main /tmp/main.c:88
  This frame has 3 object(s):
    [48, 52) 'i' (line 89)
    [64, 68) 'n' (line 89)
    [80, 180) 'b' (line 90) <== Memory access at offset 81 is inside this variable
HINT: this may be a false positive if your program uses some custom stack unwind mechanism, swapcontext or vfork
      (longjmp and C++ exceptions *are* supported)
SUMMARY: AddressSanitizer: strcpy-param-overlap /build/gcc/src/gcc/libsanitizer/asan/asan_interceptors.cpp:438 in __interceptor_strcpy
==610718==ABORTING
$
 | 
 |