改变了c程序中的注释,导致分割错误



这是提交之间的区别,这只是不同的注释和行间距,但是旧的提交正常运行,新的提交导致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);

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

expected:正常运行get: segmentation fault (core dump)

声明"这基本上是相同的程序,只是格式和注释不同"是荒谬的。提交更改:

false,
true

:

true,
false

,结果falsetrue的值分别为0和1,分别变为1和0。

代码声明openedbool,并在if (opened)中使用它。这显然对opened使用了错误的含义。

最新更新