更改了 C 程序中的注释及其导致的分段错误

0us*_*nou -8 c gcc clang gnu-make segmentation-fault

这是提交之间的区别,只是注释和行间距不同,但旧的提交正常运行,而新的结果导致段错误:

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);
Run Code Online (Sandbox Code Playgroud)

它基本上是相同的程序,只是格式和注释不同,根本没有意义。我的代码没有任何差异或错误,如果可以,请与我们分享!

预期:正常运行得到:分段错误(核心已转储)

Eri*_*hil 5

语句 \xe2\x80\x9cIt 基本上是相同的程序,只是格式和注释不同 \xe2\x80\x9d 是可笑的。提交更改:

\n
   false,\n   true\n
Run Code Online (Sandbox Code Playgroud)\n

到:

\n
   true,\n   false\n
Run Code Online (Sandbox Code Playgroud)\n

结果是 0 和 1 的值false分别true更改为 1 和 0。

\n

代码声明openedbool并在 中使用它if (opened)。这显然用错了意义opened

\n

  • 另请注意,标准 C 有一个内置类型“_Bool”,并且“#include &lt;stdbool.h&gt;”定义了“true”、“false”和“bool”。应该使用这些而不是自制定义。在 C23 中,这些被提升为完整关键字,并且标头变得过时。 (5认同)
  • 还有更多。`if (argc &lt; 2)` 变为 `if (argc != 2)` 等等。 (3认同)