首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何解决堆缓冲区溢出问题

如何解决堆缓冲区溢出问题
EN

Stack Overflow用户
提问于 2022-12-03 17:32:26
回答 1查看 44关注 0票数 0

我很难用我的程序解决问题。它看起来像它应该运行的那样。但是,当我用adress杀菌剂编译它时,我会得到堆缓冲区溢出。该函数获得一个2d数组,该数组包含一个要查找的单词--替换a和它旁边的一个单词,稍后将用于替换它。

代码语言:javascript
复制
const char * findInArray(const char * (*replace)[2], char * start) // function goes through a array 'start' and searches if the word is also in 'replace'
{
  
  char * copy = (char *) malloc (strlen(start)+1); // I made a copy of a field, so I do not modify the one I passed to function
  char * total = (char *) malloc (strlen(start)+2); // Here I will add words from copy one by one

  memmove(copy,start,strlen(start)+1); // I fill the copy array

  char * tokens = strtok (copy," "); // I split copy array 
  while (tokens != NULL)
  {
  printf("%lu ",strlen(total));
  memmove(total+strlen(total)+1,tokens,strlen(tokens)); // I add words to a new array                                                                    one by one
  memmove (total + strlen(total)," ",1); // at the end of each word I add space
  for (int i = 0 ; replace[i][0] != NULL; i++) // I search if word is in array or not, if yes I return its adress
    {const char *ptr = strstr(total,replace[i][0]); 
    if (ptr != NULL) // If there is match - I return pointer to a word that will be replaced
      {
        free (total);
        return replace[i][0];
      }
    }
    //printf("%s\n",tokens);
    tokens = strtok(NULL, " ");
  }
  free (total);
  return NULL;
  
}

如果我正确理解它的话,问题是当它已经被分配时,我会将值读取到总计数组--这对我来说没有多大意义。以下是错误消息:

代码语言:javascript
复制
==58229==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x000104d00bbb at pc 0x000102c7468c bp 0x00016d562b20 sp 0x00016d5622d8
READ of size 44 at 0x000104d00bbb thread T0
    #0 0x102c74688 in wrap_strlen+0x164 (libclang_rt.asan_osx_dynamic.dylib:arm64e+0x14688)
    #1 0x10289e4dc in findInArray test 4.c:39 // the line of moving memory to total
    #2 0x10289ee14 in newSpeak test 4.c:129
    #3 0x10289f880 in main test 4.c:186
    #4 0x1b4df7e4c  (<unknown module>)

0x000104d00bbb is located 0 bytes to the right of 43-byte region [0x000104d00b90,0x000104d00bbb)
allocated by thread T0 here:
    #0 0x102c9eca8 in wrap_malloc+0x94 (libclang_rt.asan_osx_dynamic.dylib:arm64e+0x3eca8)
    #1 0x10289e480 in findInArray test 4.c:31 // the line of allocating total array
    #2 0x10289ee14 in newSpeak test 4.c:129
    #3 0x10289f880 in main test 4.c:186
    #4 0x1b4df7e4c  (<unknown module>)

谢谢你的帮助。

EN

回答 1

Stack Overflow用户

发布于 2022-12-03 22:49:42

看起来问题在于如何为整个数组分配内存。在char * total = (char *) malloc (strlen( start )+2)行中,通过在开始长度中添加2来分配内存。但是,在memmove行(total+strlen(总计)+1,tokens,strlen(令牌))中,通过将1添加到它的长度中,将一个单词添加到总计中。这意味着total将没有足够的分配内存来存储添加的单词,并且您将得到堆缓冲区溢出错误。

要解决这个问题,您可以在添加一个新单词时将总数的长度增加2,或者只需分配足够的内存来存储整个开始字符串和添加的单词。例如:

代码语言:javascript
复制
char * total = (char *) malloc (strlen(start)+strlen(tokens)+1); // Allocate enough memory for 'total'

// Add the word to 'total'
memmove(total+strlen(total),tokens,strlen(tokens)+1); // Add the word to 'total'
memmove (total + strlen(total)," ",1); // Add a space at the end

这将修复堆缓冲区溢出错误,并允许程序正确运行。请注意,您还可能希望在使用分配的内存之前检查malloc的返回值,以确保它不是空值,以避免进一步的问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74669054

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档