Ich versuche, die Größe einiger Schichten in einer .gcode-Datei für 3D-Drucker in Bytes zu finden. Es gibt jedoch einen Fehler, den ich von dem Ausführen einer Funktion erhalte, um den Abstand zwischen zwei Instanzen der Zeichenfolge "; LAYER: *" zu finden. Hier ist meine Funktion Quelle:Segmentierungsfehler mit memmem()
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 file\n",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 file\n",layerstr);
return NULL;
}
printf("Found \"%s\" at 0x%08lX\n",layerstr,layerfind - file);
if(lastLayerfind != 0) {
tmpfind = (uint8_t*)(layerfind - file);
layerLen[i] = tmpfind - lastLayerfind;
printf("Length of layer block: 0x%X bytes\n",layerLen[i]);
}
lastLayerfind = (uint8_t*)(layerfind - file);
}
return "blah";
}
Die addVars() Funktion ist wie folgt:
char* addVars(char *s1, char *s2) {
char *result = malloc(strlen(s1)+strlen(s2)+1);
strcpy(result, s1);
strcat(result, s2);
return result;
}
Dieser Fehler nur auftreten, scheint, wenn ich versuche, mehr als zwei Schichten in int count
zu verarbeiten. Das ist normal Programmausgabe:
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
Mein GDB ist aus irgendeinem Grund nicht mehr, so dass ich bin mit LLDB und das ist, was LLDB sagt mir:
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)
So, nach LLDB, ich weiß, dass das Problem darin zu liegen scheint, auf memmem zuzugreifen. Jede Hilfe würde sehr geschätzt werden.
Was passierte also, als Sie Ihren Code mit 'lldb' durchschritten haben? Es scheint mir eine wertvolle Fähigkeit zu sein, zu lernen. Ich würde mir die 'int layerLen [] = {0};', 'für (int i = 0; i <= count; i ++) {', 'layerLen [i] = tmpfind - lastLayerfind;' ansehen Sequenz. Die Definition erlaubt nur ein "int", was je nach dem Wert von "count" zu Problemen führen kann. – paxdiablo
Ah! Das war es, danke! Und ja, ich habe gerade einen Reiseführer in einem anderen Tab geöffnet. – Dayt0n
Sie sollten in [richtige C-Formatierung] (// prohackr112.tk/r/proper-c-formatting) schauen. Oder lerne, wie du deinen Code gründlich verschleiern kannst (// prohackr112.tk/r/proper-c-obfuscation). –