Strange behavior of Ubuntu 9.04 with gcc-4.3

Malcolm malcolm.parsons at gmail.com
Tue Sep 15 11:06:28 BST 2009


2009/9/15 Guillem Salas <guillemsalas at gmail.com>:

> This little program reads from a file some records (year and name).

There is a bug in your program, here's a patch:

diff --git a/prova.c b/prova.c
index 98d493b..42f33d8 100644
--- a/prova.c
+++ b/prova.c
@@ -66,7 +66,7 @@ int LeerDiscos( char* NomFichero, AlgoAd
      return 1;
    }
    /* Alliberem memòria per al titol */
-   A->regs[i].name = malloc(sizeof(char)*strlen(tmp));
+   A->regs[i].name = malloc(sizeof(char)*(strlen(tmp)+1));
    if (A->regs[i].name == NULL) {
      perror("Problem allocating memory\n");
      return 1;

This is how I found it:

> gcc -Wall -g prova.c -o test
> valgrind ./test
==28849== Memcheck, a memory error detector.
==28849== Copyright (C) 2002-2006, and GNU GPL'd, by Julian Seward et al.
==28849== Using LibVEX rev 1658, a library for dynamic binary translation.
==28849== Copyright (C) 2004-2006, and GNU GPL'd, by OpenWorks LLP.
==28849== Using valgrind-3.2.2.SVN, a dynamic binary instrumentation framework.
==28849== Copyright (C) 2000-2006, and GNU GPL'd, by Julian Seward et al.
==28849== For more details, rerun with: -v
==28849==
==28849== Invalid write of size 1
==28849==    at 0x4C23E35: strcpy (in
/usr/lib64/valgrind/amd64-linux/vgpreload_memcheck.so)
==28849==    by 0x400A62: LeerDiscos (prova.c:74)
==28849==    by 0x400C64: main (prova.c:117)
==28849==  Address 0x4051334 is 0 bytes after a block of size 28 alloc'd
==28849==    at 0x4C22889: malloc (in
/usr/lib64/valgrind/amd64-linux/vgpreload_memcheck.so)
==28849==    by 0x4009FF: LeerDiscos (prova.c:69)
==28849==    by 0x400C64: main (prova.c:117)
Total de registres existents: 5.

-- 
Malcolm Parsons



More information about the ubuntu-devel mailing list