* glibc detected * ./parse: malloc(): smallbin double linked list corrupted:该如何处理


* glibc detected * ./parse: malloc(): smallbin double linked list corrupted:
ubuntu下写了一段文本分析的代码,比如长,如下:

  C/C++ code

   #include "../unp.h" #include <sys/mman.h> #define GET "GET " #define HOST "Host: " #define REFERER "Referer: " #define HTTP "http://" #define DATABLOCKSIZE 10000 char * filemapptr; struct datablock { char * ptr; ssizet size; }datablock[DATABLOCKSIZE], * datablockptr; struct urlreferer { char * url; char * referer; }urlreferer[DATABLOCKSIZE]; void initdatablock(const void * fileptr, ssizet filesize, struct datablock * block, ssizet blocksize); char * memstr(const char * haystack, const char * needle, sizet len); void geturlrefer(struct datablock * block, ssizet len); int main(int argc, char * argv) { int fd, i; struct stat file_stat; if (argc != 2) { err_quit("Usage: %s <filename>", argv[0]); } fd = open(argv[1], O_RDONLY); if (fd < 0) { err_sys("Open file %s error", argv[1]); } if (fstat(fd, &file_stat) < 0) { err_sys("fstat error"); } printf("file size: %d K\n", (int)(file_stat.st_size / 1024)); file_map_ptr = mmap(NULL, file_stat.st_size, PROT_READ, MAP_SHARED, fd, 0); if (file_map_ptr == MAP_FAILED) { err_sys("map failed"); } printf("file mapped success\n"); data_block_ptr = NULL; memset(data_block, 0, sizeof(data_block)); init_data_block(file_map_ptr, file_stat.st_size, data_block, DATA_BLOCK_SIZE); for (i = 0; i < DATA_BLOCK_SIZE; ++i) { if (data_block[i].ptr == NULL) break; else { printf("%s\n", data_block[i].ptr); printf("strlen() = %d\n", strlen(data_block[i].ptr)); printf("calculated size: %d\n", data_block[i].size); } } printf("block size: %d\n", i); get_url_refer(data_block, DATA_BLOCK_SIZE); return 0; } char * memstr(const char * haystack, const char * needle, size_t len) { char * p; size_t needle_len; needle_len = strlen(needle); if (haystack == NULL || needle == NULL) return NULL; for (p = (char *)haystack; p <= haystack + len - needle_len; p++) { if (memcmp(p, needle, needle_len) == 0) { return p; } } return NULL; } void init_data_block(const void * file_ptr, ssize_t file_size, struct data_block * block, ssize_t n) { char * p, * q, * t, * file_end; ssize_t i, s; file_end = (char *)file_ptr + file_size; for (i = 0, t = (char *)file_ptr; t < (char *)file_end && i < n; ++i) { p = memstr(t, GET, file_end - t); if (p) { q = memstr(p + strlen(GET), GET, file_end - p - strlen(GET)); if (q) { s = q - p; } else { s = file_end - p; } data_block[i].ptr = p; data_block[i].size = s; t = p + strlen(GET); } else break; } return ; } void get_url_refer(struct data_block * block, ssize_t len) { int i; char * p, * q, * get, * referer, * host, * url; ssize_t get_size, host_size, referer_size, url_size; memset(url_referer, 0, sizeof(url_referer)); p = q = get = referer = host = url = NULL; for (i = 0; i < len && block[i].ptr != NULL; ++i) { p = block[i].ptr; q = p + strlen(GET); while( q != ' ' && * q != '\r') q++; getsize = q - p - strlen(GET); get = (char *)malloc(getsize + 1); memset(get, 0, getsize + 1); memcpy(get, p + strlen(GET), getsize); p = memstr(block[i].ptr, HOST, block[i].size); if (p) { q = p + strlen(HOST); while(* q != '\r' && q != ' ') q++; host_size = q - p - strlen(HOST); host = (char *)calloc(host_size + 1, sizeof(char)); memcpy(host, p + strlen(HOST), host_size); } p = memstr(block[i].ptr, REFERER, block[i].size); if (p) { q = p + strlen(REFERER); while(q != '\r' && *q != ' ') q++; referersize = q - p - strlen(REFERER); referer = (char *)calloc(referersize + 1, sizeof(char)); memcpy(referer, p + strlen(REFERER), referersize); } urlsize = hostsize + getsize + strlen(HTTP); url = (char *)calloc(urlsize + 1, sizeof(char)); memcpy(url, HTTP, strlen(HTTP)); memcpy(url + strlen(HTTP), host, hostsize); memcpy(url + strlen(HTTP) + hostsize, get, getsize); [color=#FF0000] free(host); printf("free(host) ok\n"); free(get); printf("free(get) ok\n"); [/color] urlreferer[i].url = url; urlreferer[i].referer = referer; printf("url = %s\n", url); if (referer) printf("referer = %s\n", referer); else printf("referer is NULL\n"); } return ; }



相关链接

c语言 基本概念 程序开发

滴血的内脏 12 years, 9 months ago

Your Answer