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)
它基本上是相同的程序,只是格式和注释不同,根本没有意义。我的代码没有任何差异或错误,如果可以,请与我们分享!
预期:正常运行得到:分段错误(核心已转储)
语句 \xe2\x80\x9cIt 基本上是相同的程序,只是格式和注释不同 \xe2\x80\x9d 是可笑的。提交更改:
\n false,\n true\nRun Code Online (Sandbox Code Playgroud)\n到:
\n true,\n false\nRun Code Online (Sandbox Code Playgroud)\n结果是 0 和 1 的值false分别true更改为 1 和 0。
代码声明opened为bool并在 中使用它if (opened)。这显然用错了意义opened。