Valgrind:大小 1 的无效读取

Valgrind : Invalid read of size 1

我有以下代码,但我无法弄清楚为什么我会从 valgrind 收到此错误 (对于上下文,我试图将文件中的文本复制到字符串变量中)

int main(int argc, char **argv)
{
  FILE *fp;
  int f_size;
  char *string;

  fp = fopen("./src/input.txt", "rw+");
  if (fp == NULL)
  {
    printf("Error!");
    exit(1);
  }

  fseek(fp, 0, SEEK_END);
  f_size = ftell(fp);
  fseek(fp, 0, SEEK_SET);

  string = (char *)malloc(sizeof(char) * f_size);
  fread(string, sizeof(char), f_size, fp);
  printf("\n%s\n", string);
  
  free(string);
  fclose(fp);
  return 0;
} /* main */

我通过 printf 得到了以下错误

==19085== Memcheck, a memory error detector
==19085== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==19085== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==19085== Command: ./build/find_word
==19085== 

==19085== Invalid read of size 1
==19085==    at 0x483EF54: strlen (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==19085==    by 0x48CEE94: __vfprintf_internal (vfprintf-internal.c:1688)
==19085==    by 0x48B7EBE: printf (printf.c:33)
==19085==    by 0x10958C: main (main.c:44)
==19085==  Address 0x4a4932e is 0 bytes after a block of size 142 alloc'd
==19085==    at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==19085==    by 0x109555: main (main.c:42)
==19085== 
text from file
==19085== 
==19085== HEAP SUMMARY:
==19085==     in use at exit: 0 bytes in 0 blocks
==19085==   total heap usage: 4 allocs, 4 frees, 5,734 bytes allocated
==19085== 
==19085== All heap blocks were freed -- no leaks are possible
==19085== 
==19085== For lists of detected and suppressed errors, rerun with: -s
==19085== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

您分配的缓冲区足够大,可以容纳从文件中读取的字符,但不能容纳其他内容。假设文件只包含文本,这意味着 string 实际上不是字符串,因为没有终止空字节。因此,当您将它传递给 printf 时,它会读取已分配内存的末尾,这就是 valgrind 告诉您的内容。

您需要将分配的内存量加 1,并在填充缓冲区后手动将 0 写入最后一个字节以获得正确的字符串。

string = malloc(sizeof(char) * f_size + 1);
fread(string, sizeof(char), f_size, fp);
string[f_size] = 0;

您需要一个额外的字节来存储空字符。

string = malloc(sizeof(char) * f_size + 1);

读取后终止数组,如下所示。您可以使用 fread 的 return 值来终止字符串。

size_t ret = fread(string, sizeof(char), f_size, fp);
if (ret != -1) {
   string[ret] = 0;
} else {
   //Error
}