释放 malloc 指针时出错

Error while freeing a malloc'd pointer

我在以下代码段中释放 malloc 字符串时出错:

long authenticate(char* user, long* uid, long* gid){
FILE* fp = fopen("/etc/passwd", "r");
char * line = NULL;
size_t len = 0;
long lineSize;
char* uname;

if (fp == NULL)
  exit(EXIT_FAILURE);

  while ((lineSize = getline(&line, &len, fp)) != -1) {

    uname = (char*)malloc(lineSize*sizeof(char));
    getInfo(line, lineSize, &uname, &uid, &gid);

    if(strcmp(uname, user) == 0) {  // Strings are equal
      printf("we've found a user!\n");
      printf("uname: %s uid: %ld gid: %ld\n", uname, *uid, *gid);
      free(uname);
      exit(1);
    }

    free(uname);
  }
}

每当我找到有效的用户名时,释放 uname 然后退出什么都没有发生。但是,当我在循环结束时尝试释放 uname 时找不到有效的 uname(如果语句未被评估),我会收到以下错误:

malloc: *** error for object 0x10d20ef2c: pointer being freed was not allocated

我不明白为什么 free() 没有注册 uname 之前 malloc'd。鉴于错误的性质,我认为问题是我不了解 malloc/free.

问题很可能是 getInfo() 修改了 uname 指向的位置,之后 free() 无法识别它。