Valgrind读/写的大小无效1

时间:2016-10-17 21:59:41

标签: valgrind

所以我正在编写一个程序,将2个字符串组合成1,程序正在工作,但我使用了valgrind并得到了很多警告,但我不知道什么是错的,因为我使用ubuntu几天而且几乎不知道valgrind。     #包括     #包括     #include

char* fun (char* n1, char* n2);

int main(){

  char* string1 = "This home ";
  char* string2 = "is big";
  char* next;

  printf("Sklejony napis: %s\n", next=fun(string1, string2));
  free(next);
return 0;
}

char* fun (char* n1, char* n2)
{


  char* string3 = malloc(sizeof(char));



  int index_first = 0, index_second = 0;

  for(; n1[index_first] != '\0'; index_first++)
  {
    string3[index_first] = n1[index_first];

  }

  int index_third = index_first;

  for(; n2[index_second] != '\0'; index_second++)
  {

    string3[index_third] = n2[index_second];
    index_third++;

  }

  return string3;
}

valgrind说:

Invalid write of size 1
==2402==    at 0x400659: fun (plik.c:30)
==2402==    by 0x4005E0: main (plik.c:13)
==2402==  Address 0x5203041 is 0 bytes after a block of size 1 alloc'd
==2402==    at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2402==    by 0x400627: fun (plik.c:22)
==2402==    by 0x4005E0: main (plik.c:13)
==2402==
==2402== Invalid write of size 1
==2402==    at 0x400698: fun (plik.c:39)
==2402==    by 0x4005E0: main (plik.c:13)
==2402==  Address 0x520304a is 9 bytes after a block of size 1 alloc'd
==2402==    at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2402==    by 0x400627: fun (plik.c:22)
==2402==    by 0x4005E0: main (plik.c:13)
==2402==
==2402== Invalid read of size 1
==2402==    at 0x4E88C80: vfprintf (vfprintf.c:1632)
==2402==    by 0x4E8F848: printf (printf.c:33)
==2402==    by 0x4005FA: main (plik.c:13)
==2402==  Address 0x5203041 is 0 bytes after a block of size 1 alloc'd
==2402==    at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2402==    by 0x400627: fun (plik.c:22)
==2402==    by 0x4005E0: main (plik.c:13)
==2402==
==2402== Invalid read of size 1
==2402==    at 0x4EB321D: _IO_file_xsputn@@GLIBC_2.2.5 (fileops.c:1301)
==2402==    by 0x4E884CA: vfprintf (vfprintf.c:1632)
==2402==    by 0x4E8F848: printf (printf.c:33)
==2402==    by 0x4005FA: main (plik.c:13)
==2402==  Address 0x520304f is 14 bytes after a block of size 1 alloc'd
==2402==    at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2402==    by 0x400627: fun (plik.c:22)
==2402==    by 0x4005E0: main (plik.c:13)
==2402==
==2402== Invalid read of size 1
==2402==    at 0x4EB3234: _IO_file_xsputn@@GLIBC_2.2.5 (fileops.c:1301)
==2402==    by 0x4E884CA: vfprintf (vfprintf.c:1632)
==2402==    by 0x4E8F848: printf (printf.c:33)
==2402==    by 0x4005FA: main (plik.c:13)
==2402==  Address 0x520304e is 13 bytes after a block of size 1 alloc'd
==2402==    at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2402==    by 0x400627: fun (plik.c:22)
==2402==    by 0x4005E0: main (plik.c:13)
==2402==
==2402== Invalid read of size 1
==2402==    at 0x4C35030: __GI_mempcpy (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2402==    by 0x4EB3171: _IO_file_xsputn@@GLIBC_2.2.5 (fileops.c:1319)
==2402==    by 0x4E884CA: vfprintf (vfprintf.c:1632)
==2402==    by 0x4E8F848: printf (printf.c:33)
==2402==    by 0x4005FA: main (plik.c:13)
==2402==  Address 0x520304f is 14 bytes after a block of size 1 alloc'd
==2402==    at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2402==    by 0x400627: fun (plik.c:22)
==2402==    by 0x4005E0: main (plik.c:13)
==2402==
==2402== Invalid read of size 1
==2402==    at 0x4C35040: __GI_mempcpy (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2402==    by 0x4EB3171: _IO_file_xsputn@@GLIBC_2.2.5 (fileops.c:1319)
==2402==    by 0x4E884CA: vfprintf (vfprintf.c:1632)
==2402==    by 0x4E8F848: printf (printf.c:33)
==2402==    by 0x4005FA: main (plik.c:13)
==2402==  Address 0x520304d is 12 bytes after a block of size 1 alloc'd
==2402==    at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2402==    by 0x400627: fun (plik.c:22)
==2402==    by 0x4005E0: main (plik.c:13)

2 个答案:

答案 0 :(得分:0)

当你想要连接它们时,你必须malloc那么多包含两个字符串的空间,或者使用字符串库,其中是函数concat。

///you dont need to specify sizeof because of char is 1 byte information
string3 = malloc (strlen (n1) + strlen (n2) + 1);

/* the code is working there*/
for(; n2[index_second] != '\0'; index_second++)
{
 string3[index_third] = n2[index_second];
 index_third++;
}

///there you have to add \0 terminator at end of 
///function because of loop it is going only to \0
string3[index_third]='\0';

答案 1 :(得分:0)

func ServeHTTP(w http.ResponseWriter, r *http.Request) {
    w.WriteHeader(http.StatusInternalServerError)
    w.Write([]byte("500 - Something bad happened!"))
}