Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在C运行时获取哪些有问题的源代码和代码行?_C_Pointers_Runtime Error_Static Analysis_Dereference - Fatal编程技术网

如何在C运行时获取哪些有问题的源代码和代码行?

如何在C运行时获取哪些有问题的源代码和代码行?,c,pointers,runtime-error,static-analysis,dereference,C,Pointers,Runtime Error,Static Analysis,Dereference,我在ubuntu中使用这个和(uthash dev) 直接运行(使用valgrind-s-轨道原点=yes-保持stacktraces=alloc和free检查时没有问题): 使用gcc时(使用-fstack protector all标志时也没有运行时错误): 使用clang时(使用-fsanize=address标志时也没有运行时错误): 只有在使用tcc时,它在运行时显示错误,我怎么知道是哪个行号导致的?注意:to\u rhex(0)返回”,而不是“0”。使用do{}while()to\u

我在ubuntu中使用这个和(
uthash dev

直接运行(使用
valgrind-s-轨道原点=yes-保持stacktraces=alloc和free检查时没有问题
):

使用gcc时(使用
-fstack protector all
标志时也没有运行时错误):

使用clang时(使用
-fsanize=address
标志时也没有运行时错误):


只有在使用
tcc
时,它在运行时显示错误,我怎么知道是哪个行号导致的?

注意:
to\u rhex(0)
返回
,而不是
“0”
。使用
do{}while()
to\u rhex(有些是负数)
-->
”。很好,谢谢@Restore,Updated注意:
to\u rhex(0)
返回
,而不是
“0”
。使用
do{}while()
致rhex(有些是负数)
-->
”。很好,谢谢@Restore,已更新
// uthash.c
#include <string.h>  /* strcpy */
#include <stdlib.h>  /* malloc */
#include <stdio.h>   /* printf */
#include "uthash.h"

const int MAX_DATA = 1230000;

struct kv {
    char *key;
    double val;
    UT_hash_handle hh;
};

char i2ch[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'B', 'c', 'D', 'e', 'F'};

int get_first_digit(double d) {
    while(d > 10) {
        d /= 10;
    }
    return (int)(d);
}
char* to_rhex(int v) {
    char* hex = malloc(sizeof(char)*9); // because INT_MAX can be divided by 16 maximum 8 times
    memset(hex,0,9);
    int ctr = 0;
    do {
        hex[ctr++] = i2ch[v%16];
        v /= 16;
    } while(v > 0);
    return hex;
}

char* to_str(int v) {
    int len = snprintf(NULL,0,"%d",v);
    char *res = malloc(len+1);
    snprintf(res, len+1, "%d", v);  
    return res;
}

int set_or_inc(struct kv** m, char* key, int set, int inc, int *ctr) {
    struct kv* item = 0;
    HASH_FIND_STR(*m, key, item);
    if(!item) {
        item = malloc(sizeof(*item));
        item->key = key;
        item->val = (double)(set);
        HASH_ADD_KEYPTR(hh, *m, item->key, strlen(item->key), item) ;
        return 0;
    } else {
        item->val += (double)(inc);
        *ctr += 1;
        return 1; // key not used
    }
}
int main() {
    struct kv *m = NULL;
    int dup1 =0, dup2 =0, dup3 =0;
    for(int z = MAX_DATA; z > 0; z--) {
        int val2 = MAX_DATA - z;
        int val3 = MAX_DATA*2 - z;
        char *key1 = to_str(z);
        char *key2 = to_str(val2);
        char *key3 = to_rhex(val3);
        if(set_or_inc(&m, key1, z, val2, &dup1)) free(key1);
        if(set_or_inc(&m, key2, val2, val3, &dup2)) free(key2);
        if(set_or_inc(&m, key3, val3, z, &dup3)) free(key3);
    }
    printf("%d %d %d\n",dup1, dup2, dup3);
    int total = 0, verify = 0, count = 0;
    struct kv *tmp, *item;
    HASH_ITER(hh, m, item, tmp) {
        total += get_first_digit(item->val);
        verify += strlen(item->key);
        count += 1;
        HASH_DEL(m,item);
        free(item->key);
        free(item);
    }
    printf("%d %d %d\n",total, verify, count);
}
time tcc -b -g -run uthash.c
bcheck.c __bound_ptr_indir8: 0x7fff3cd67ec8 is outside of the region
Runtime error: dereferencing invalid pointer
at 0x5626cfebe9b5 set_or_inc()
by 0x5626cfec15e9 main()
Command exited with non-zero status 255

CPU: 0.00s      Real: 0.00s     RAM: 3480KB
time tcc -run uthash.c
637912 641149 67002
3808703 14182513 2343937
Command exited with non-zero status 25

CPU: 2.52s      Real: 2.60s     RAM: 292144KB
gcc uthash.c && time ./a.out
637912 641149 67002
3808703 14182513 2343937

CPU: 2.47s      Real: 2.57s     RAM: 290492KB
clang uthash.c && time ./a.out
637912 641149 67002
3808703 14182513 2343937

CPU: 2.50s      Real: 2.59s     RAM: 290448KB