为什么 fread() 在此代码中抛出 'segmentation fault'
Why fread() throwing the 'segmentation fault' in this code
该程序是检查raw文件和识别jpg文件。原始文件也被格式化为 FAT,因此我必须将其检查为 512 字节块。
我用 BYTE fats[512];
制作了 (?) 512 字节内存 space 并在 fread()
中用作目标内存槽,但在此过程中它抛出 'segmentation fault' 错误.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
typedef uint8_t BYTE;
int main(int argc, char *argv[])
{
if (argc != 2)
{
printf("Usage: ./recover image\n");
return 1;
}
FILE *dmgdFile = fopen(argv[1], "rb");
if (dmgdFile == NULL)
{
printf("Could not open file.\n");
return 1;
}
char name[7];
int counter = 0;
int discovered = 0;
FILE *outptr;
BYTE fats[512];
sprintf(name, "%03d.jpg", discovered);//name regeneration
while (fread(&fats, sizeof(fats), 1, dmgdFile)){
if (
discovered > 0 && //DISCOVERED ANOTHER JPG
fats[0] == 0xff &&
fats[1] == 0xd8 &&
fats[2] == 0xff &&
fats[3] >= 0xe0 && fats[3] <= 0xef) {
printf("repeated discovery");
fclose(outptr);
sprintf(name, "%03d.jpg", discovered);//name regeneration
outptr = fopen(name, "w"); //create next jpg file and open pointer
if (outptr == NULL)
{
fclose(dmgdFile);
fprintf(stderr, "Could not create %s.\n", name);
return 5;
}
counter ++;
discovered ++;
} else if(discovered > 0){ //ADDING JPG BODY TO PREVIOUS OPENED FILE
printf("body");
fwrite(fats, sizeof(fats), 1, outptr);
counter ++;
}else if (
discovered == 0 && //FIRST TIME DISCOVERING JPG HEADER
fats[0] == 0xff &&
fats[1] == 0xd8 &&
fats[2] == 0xff &&
fats[3] >= 0xe0 && fats[3] <= 0xef) {
printf("1st time discovered");
outptr = fopen(name, "w");
if (outptr == NULL)
{
fclose(dmgdFile);
fprintf(stderr, "Could not create %s.\n", name);
return 5;
}
fwrite(fats, sizeof(fats), 1, outptr);
counter ++;
discovered ++;
}
}
fclose(dmgdFile);
}
我在 'fread()' 中尝试过使用和不使用“&”的脂肪,但没有用。
sprintf(name, "%03d.jpg", discovered);
会将至少7个字符的字符串写入name
,但数组name
只有7个元素。考虑到终止空字符,这将导致超出范围的访问。看起来这种越界访问破坏了文件指针并导致了分段错误。
分配足够的元素并使用 snprintf()
来避免像这样的缓冲区溢出:
char name[32];
snprintf(name, sizeof(name), "%03d.jpg", discovered);
该程序是检查raw文件和识别jpg文件。原始文件也被格式化为 FAT,因此我必须将其检查为 512 字节块。
我用 BYTE fats[512];
制作了 (?) 512 字节内存 space 并在 fread()
中用作目标内存槽,但在此过程中它抛出 'segmentation fault' 错误.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
typedef uint8_t BYTE;
int main(int argc, char *argv[])
{
if (argc != 2)
{
printf("Usage: ./recover image\n");
return 1;
}
FILE *dmgdFile = fopen(argv[1], "rb");
if (dmgdFile == NULL)
{
printf("Could not open file.\n");
return 1;
}
char name[7];
int counter = 0;
int discovered = 0;
FILE *outptr;
BYTE fats[512];
sprintf(name, "%03d.jpg", discovered);//name regeneration
while (fread(&fats, sizeof(fats), 1, dmgdFile)){
if (
discovered > 0 && //DISCOVERED ANOTHER JPG
fats[0] == 0xff &&
fats[1] == 0xd8 &&
fats[2] == 0xff &&
fats[3] >= 0xe0 && fats[3] <= 0xef) {
printf("repeated discovery");
fclose(outptr);
sprintf(name, "%03d.jpg", discovered);//name regeneration
outptr = fopen(name, "w"); //create next jpg file and open pointer
if (outptr == NULL)
{
fclose(dmgdFile);
fprintf(stderr, "Could not create %s.\n", name);
return 5;
}
counter ++;
discovered ++;
} else if(discovered > 0){ //ADDING JPG BODY TO PREVIOUS OPENED FILE
printf("body");
fwrite(fats, sizeof(fats), 1, outptr);
counter ++;
}else if (
discovered == 0 && //FIRST TIME DISCOVERING JPG HEADER
fats[0] == 0xff &&
fats[1] == 0xd8 &&
fats[2] == 0xff &&
fats[3] >= 0xe0 && fats[3] <= 0xef) {
printf("1st time discovered");
outptr = fopen(name, "w");
if (outptr == NULL)
{
fclose(dmgdFile);
fprintf(stderr, "Could not create %s.\n", name);
return 5;
}
fwrite(fats, sizeof(fats), 1, outptr);
counter ++;
discovered ++;
}
}
fclose(dmgdFile);
}
我在 'fread()' 中尝试过使用和不使用“&”的脂肪,但没有用。
sprintf(name, "%03d.jpg", discovered);
会将至少7个字符的字符串写入name
,但数组name
只有7个元素。考虑到终止空字符,这将导致超出范围的访问。看起来这种越界访问破坏了文件指针并导致了分段错误。
分配足够的元素并使用 snprintf()
来避免像这样的缓冲区溢出:
char name[32];
snprintf(name, sizeof(name), "%03d.jpg", discovered);