Article 54D84 compiler claims memory that was already malloc-ed was not malloc-ed

compiler claims memory that was already malloc-ed was not malloc-ed

by
andrew.comly
from LinuxQuestions.org on (#54D84)
I malloc-ed a string "string", and then I freed it, but I get this strange error message:
Code:==8335==ERROR: AddressSanitizer: attempting free on address which was not malloc()-ed: 0x7fffffffe41e in thread T0
#0 0x4b7db0 (/media/a/LG/AC/Learn/Programming/C/arrays/a.out+0x4b7db0)
#1 0x4e97c4 (/media/a/LG/AC/Learn/Programming/C/arrays/a.out+0x4e97c4)
#2 0x7ffff6ee582f (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)
#3 0x417e08 (/media/a/LG/AC/Learn/Programming/C/arrays/a.out+0x417e08)

AddressSanitizer can not describe address in more detail (wild memory access suspected).
SUMMARY: AddressSanitizer: bad-free (/media/a/LG/AC/Learn/Programming/C/arrays/a.out+0x4b7db0)
==8335==ABORTING
[Inferior 1 (process 8335) exited with code 01]
(gdb)My code is below:
string_simple.c
Code:#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "printStr.h"
#include "strIsEmpty.h"

int main(int argc, char ** argv)
{
//Verify User input non-empty string
if(strIsEmpty(argv[1]))
{
printf("String NOT detected..\n Please rerun program and enter a string\n");
exit(EXIT_FAILURE);
}

int length = strlen(argv[1]);

//Allocate memory for array in heap
char* string = (char*)malloc((length + 1) * sizeof(char));
string = argv[1];

//Padding
string[length] = '\0';

//Print out char array
printStr(string);

//Free
free(string);
}
printStr.h
Code: void printStr(char* letter)
{
printf("%s\n", letter);
printf("\n");
}
strIsEmpty.h
Code: short int strIsEmpty(char * str)
{
if(str == NULL)
{
return(1);
}
else
{
return(0);
}
}Subject code successfully compiles with:
Code:$ clang -g -std=c99 -Wall -Wvla -Werror -fsanitize=address,undefined ./string_simple.cbut when I actually run it with an argument:
Code:/arrays$ ./a.out asdfuieI get the aforesaid error.

File stats are:
Code:.../arrays$ ls -l string_simple.c printStr.h strIsEmpty.h
-rw-rw-r-- 1 a a 77 Jun 6 02:52 printStr.h
-rw-rw-r-- 1 a a 580 Jun 6 14:17 string_simple.c
-rw-rw-r-- 1 a a 98 Jun 6 12:14 strIsEmpty.hI tried looking on web, the only thing remotely related didn't really seem to directly have to do with my situation:
1) stackoverflow
2) github
3) hoelz.ro/blog

I run the above in gdb, but even though I step through main line by line, still nothing can be seen. The only thing that I can see is that only after the very last line of the code:
Code: free(string);do I get the above error.

Why do I get this error? Aren't we supposed to free anything malloc-ed?latest?d=yIl2AUoC8zA latest?i=JhSE80yQRRQ:oLFwXa7flLA:F7zBnMy latest?i=JhSE80yQRRQ:oLFwXa7flLA:V_sGLiP latest?d=qj6IDK7rITs latest?i=JhSE80yQRRQ:oLFwXa7flLA:gIN9vFwJhSE80yQRRQ
External Content
Source RSS or Atom Feed
Feed Location https://feeds.feedburner.com/linuxquestions/latest
Feed Title LinuxQuestions.org
Feed Link https://www.linuxquestions.org/questions/
Reply 0 comments