C- memmem()的分割故障



我试图在3D打印机的.gcode文件中找到某些层的字节中的大小。但是,我从运行一个函数中遇到一个错误,以找到字符串的两个实例之间的距离"; layer:*"。这是我的功能来源:

char* storeLayers(FILE* fp, int count) {
    double size = get_filesize(fp);
    uint8_t* file = (uint8_t*)malloc(size);
    if(!file) {
        printf("Error allocating 0x%lX bytes for GCode filen",size);
        fclose(fp);
        return NULL;
    }
    int layerLen[] = {0};
    fread(file,1,size,fp);
    char* layerstr = NULL;
    char* layer = ";LAYER:";
    char layernum[100];
    char* pointerlayernum;
    uint8_t* layerfind;
    uint8_t* lastLayerfind = 0;
    uint8_t* tmpfind;
    for(int i = 0; i <= count; i++) {
        sprintf(layernum,"%d",i);
        pointerlayernum = layernum;
        // make count string
        layerstr = addVars(layer,pointerlayernum);
        printf("|%s|n",layerstr);
        layerfind = memmem(file,size,layerstr,strlen(layerstr);
        if(!layerfind) {
            printf("Unable to find %s in the filen",layerstr);
            return NULL;
        }
        printf("Found "%s" at 0x%08lXn",layerstr,layerfind - file);
        if(lastLayerfind != 0) {
            tmpfind = (uint8_t*)(layerfind - file);
            layerLen[i] = tmpfind - lastLayerfind;
            printf("Length of layer block: 0x%X bytesn",layerLen[i]);
        }
        lastLayerfind = (uint8_t*)(layerfind - file);
    }
    return "blah";
}

addvars()函数如下:

char* addVars(char *s1, char *s2) {
    char *result = malloc(strlen(s1)+strlen(s2)+1);
    strcpy(result, s1);
    strcat(result, s2);
    return result;
}

当我尝试在int count中处理超过2层时,此错误似乎发生。这是正常的程序输出:

MacBook-Pro-27:fchost dayt0n$ ./fchost -d /Users/dayt0n/Downloads/paper_bin.gcode
Layers: 509
Filament Length: 4392.00 mm
|;LAYER:0|
Found ";LAYER:0" at 0x0000035F
|;LAYER:1|
Found ";LAYER:1" at 0x00002E67
Length of layer block: 0x2B08 bytes
|;LAYER:2|
Segmentation fault: 11

我的GDB因某种奇怪的原因而被打破,所以我正在使用LLDB,这就是LLDB告诉我的:

MacBook-Pro-27:fchost dayt0n$ lldb fchost
(lldb) target create "fchost"
Current executable set to 'fchost' (x86_64).
(lldb) r -d /Users/dayt0n/Downloads/paper_bin.gcode /d
Process 21523 launched: '/Users/dayt0n/Github/fchost/fchost' (x86_64)
Layers: 509
Filament Length: 4392.00 mm
|;LAYER:0|
Found ";LAYER:0" at 0x0000035F
|;LAYER:1|
Found ";LAYER:1" at 0x00002E67
Length of layer block: 0x2B08 bytes
|;LAYER:2|
Process 21523 stopped
* thread #1: tid = 0xf706b, 0x00007fffeaa8338b libsystem_c.dylib`memmem + 104, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=2, address=0x100093000)
    frame #0: 0x00007fffeaa8338b libsystem_c.dylib`memmem + 104
libsystem_c.dylib`memmem:
->  0x7fffeaa8338b <+104>: movzbl (%rbx), %eax
    0x7fffeaa8338e <+107>: cmpl   %r13d, %eax
    0x7fffeaa83391 <+110>: jne    0x7fffeaa833a5            ; <+130>
    0x7fffeaa83393 <+112>: movq   %rbx, %rdi
(lldb)

因此,根据LLDB的说法,我知道问题似乎存在于访问MEMMEM中。任何帮助将不胜感激。

我会查看int layerLen[] = {0};for(int i = 0; i <= count; i++) {layerLen[i] = tmpfind - lastLayerfind;序列。该定义仅允许一个int因此,根据count的值,这可能会导致问题。 &ndash;&nbsp; paxdiablo

相关内容

  • 没有找到相关文章

最新更新