检测到 C 堆栈崩溃

标签 c stack stack-overflow

我有一个函数可以计算某个单词在文件中出现的次数。现在由于某种原因,它得到堆栈粉碎检测到的错误,我没有看到错误。

代码如下:

int contar_palabra(const char *nombre_file, const char *palabra){

/*variables locales*/
FILE *file; 
char buffer[50]; 
int bytes_leidos, contador = 0, comparar, cerrar, i;    

/*Abrimos el archivo para lectura*/
file = fopen(nombre_file, "r");

/*Verificamos que se haya abierto correctamente*/
if (file == NULL){
  printf("No se pudo abrir el archivo \n");
  perror(nombre_file);
  exit(EXIT_FAILURE);
}

/*Procedemos a contar cuantas veces aparece una palabra*/
while (!feof(file)){

  bytes_leidos = fscanf(file, "%s", buffer);

  if (bytes_leidos > 0){

    /*Hacemos la comparacion*/
    comparar = strcmp(buffer, palabra);
    if (comparar == 0)
      contador++;
  }
  else if(errno == EOF)     
        printf("Error al leer alguna palabra de %s \n", nombre_file);
  else if (bytes_leidos == EOF)
    break;
}

cerrar = fclose(file);

if(cerrar == EOF){
  printf("Error: no se pudo cerra el archivo.");
}

printf("antes de retornar contador \n");
return contador; 

我使用 valgrind 试图识别错误,日志文件给了我这个:

  ==2252== Memcheck, a memory error detector
  ==2252== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
  ==2252== Using Valgrind-3.6.1 and LibVEX; rerun with -h for copyright info
  ==2252== Command: ./pargrep viejo.txt
  ==2252== Parent PID: 1756
  ==2252== 
  ==2252== 
  ==2252== HEAP SUMMARY:
  ==2252==     in use at exit: 55 bytes in 1 blocks
  ==2252==   total heap usage: 7 allocs, 6 frees, 1,389 bytes allocated
  ==2252== 
  ==2252== 55 bytes in 1 blocks are still reachable in loss record 1 of 1
  ==2252==    at 0x4026864: malloc (vg_replace_malloc.c:236)
  ==2252==    by 0x40B878B: __libc_message (libc_fatal.c:138)
  ==2252==    by 0x413D09F: __fortify_fail (fortify_fail.c:32)
  ==2252==    by 0x413D049: __stack_chk_fail (stack_chk_fail.c:29)
  ==2252==    by 0x8049142: contar_palabra (in /home/alessandro/OS/Proyecto2/prueba1.0/pargrep)
  ==2252==    by 0x80489D4: main (in /home/alessandro/OS/Proyecto2/prueba1.0/pargrep)
  ==2252== 
  ==2252== LEAK SUMMARY:
  ==2252==    definitely lost: 0 bytes in 0 blocks
  ==2252==    indirectly lost: 0 bytes in 0 blocks
  ==2252==      possibly lost: 0 bytes in 0 blocks
  ==2252==    still reachable: 55 bytes in 1 blocks
  ==2252==         suppressed: 0 bytes in 0 blocks
  ==2252== 
  ==2252== For counts of detected and suppressed errors, rerun with: -v
  ==2252== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 15 from 8)

奇怪的是它在返回之前打印一条消息。 我真的没有看到错误,感谢您的帮助。 提前致谢

最佳答案

使用普通 %s 作为 fscanf 的格式说明符是非常危险的,因为您无法防止超出缓冲区末尾的写入,缓冲区末尾在您的缓冲区中只有 50 个字节案件。考虑为格式说明符提供一个宽度修饰符,尽管这将在达到更复杂的宽度限制时处理准确的计数。

您可能会发现逐个字符读取 (fgetc) 或读取固定缓冲区 (fread) 并手动检测单词分隔符会生成更简单的代码。

关于检测到 C 堆栈崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11275920/

相关文章:

c - 我的程序不打印最后一行代码的函数结果

c++ - 我的 C++ 程序在计算欧拉数的级数时遇到问题

c# - 如何防止我的 Ackerman 函数溢出堆栈?

java - 尝试递归打印整数 LinkedList 时出现堆栈溢出错误? ( java )

c - 在C中输入数组

c - 尝试对由 mmap() 生成的指针使用 memcpy() 时出现总线错误

java - LinkedListStack System.out.println

java - 我怎样才能使这些堆栈通用?

c - 为什么我的MD5 Hash在整个过程中都是静态的?

c++ - 指向 Thread 的指针数组中的 PCB 中的相同名称