c程序中注解的更改及其导致的分段错误

chhkpiq4  于 2023-01-04  发布在  其他
关注(0)|答案(1)|浏览(113)

这就是提交之间的区别,只是注解和行空间不同,但是旧的提交运行正常,而新的提交导致seg错误:

Binary files a/recover/recover and b/recover/recover differ
diff --git a/recover/recover.c b/recover/recover.c
index f0ffdf6..02ab42b 100644
--- a/recover/recover.c
+++ b/recover/recover.c
@@ -1,34 +1,32 @@
-#include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <stdint.h>
 #include <string.h>
 
 typedef enum
 {
-   false,
-   true
-} bool;
+   true,
+   false
 
-typedef uint8_t BYTE;
+} bool;
 
 typedef char *string;
 
+typedef uint8_t BYTE;
 int BLOCK_SIZE = 512;
 
 int main(int argc, char *argv[])
 {
-   // Check if a file name was provided as an argument
-   if (argc < 2)
+   if (argc != 2)
    {
-      fprintf(stderr, "Error: No file name provided.\n");
+      fprintf(stderr, "Usage: recover {filename}\n");
       return 1;
    }
-
-   // Open the file for reading
-   FILE *raw_file = fopen(argv[1], "r");
+   string fpath = argv[1] == NULL ? "./card.raw" : argv[1];
+   FILE *raw_file = fopen(fpath, "r");
    if (raw_file == NULL)
    {
-      fprintf(stderr, "Error: Could not open file.\n");
+      fprintf(stderr, "Error: filename not valid!\n");
       return 2;
    }
 
@@ -38,33 +36,31 @@ int main(int argc, char *argv[])
    FILE *img = NULL;
    bool opened = false;
 
-   // Read blocks of size BLOCK_SIZE from the file
    while (fread(buffer, 1, BLOCK_SIZE, raw_file) == BLOCK_SIZE)
    {
-      // If start of new JPEG
+      // if start of new jpeg
       if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
       {
-         // If a JPEG file is already open, close it and free the filename
+         // if opened
          if (opened)
          {
             free(fname);
             fclose(img);
          }
-         // Allocate memory for the new filename and create a new JPEG file
+         // opened or not
          fname = malloc(sizeof(char) * 8);
          sprintf(fname, "%03i.jpg", i);
          img = fopen(fname, "w");
          opened = true;
          i++;
       }
-      // If a JPEG file is open, write the block to it
+      // if opened jpeg start or not
       if (opened)
       {
          fwrite(buffer, 1, BLOCK_SIZE, img);
       }
    }
-
-   // If a JPEG file is open, close it and free the filename
+   // when gone over entire disk image
    if (opened)
    {
       free(fname);

这是一个基本相同的程序,只是格式和注解不同,这没有任何意义。我没有任何差异或错误的代码,如果可以的话,请与我们分享!
预期:正常运行得到:分段故障(核心转储)

dgtucam1

dgtucam11#

"这基本上是相同的程序,只是格式和注解不同"的说法是可笑的。

false,
   true

致:

true,
   false

结果是分别为0和1的falsetrue的值分别变为1和0。
代码将opened声明为bool,并在if (opened)中使用它,这显然对opened使用了错误的含义。

相关问题