Uninitialised value wurde durch einen heap-Zuweisung

Habe ich schon jagen, dieser bug herum, und ich verstehe es einfach nicht. Habe ich vergessen einige grundlegende C oder so etwas?

==28357== Conditional jump or move depends on uninitialised value(s)
==28357==    at 0x4C261E8: strlen (mc_replace_strmem.c:275)
==28357==    by 0x4E9280A: puts (ioputs.c:36)
==28357==    by 0x400C21: handlePath (myshell.c:105)
==28357==    by 0x400B17: handleInput (myshell.c:69)
==28357==    by 0x400AAD: acceptInput (myshell.c:60)
==28357==    by 0x4009CF: main (myshell.c:33)
==28357==  Uninitialised value was created by a heap allocation
==28357==    at 0x4C25153: malloc (vg_replace_malloc.c:195)
==28357==    by 0x400BDE: handlePath (myshell.c:99)
==28357==    by 0x400B17: handleInput (myshell.c:69)
==28357==    by 0x400AAD: acceptInput (myshell.c:60)
==28357==    by 0x4009CF: main (myshell.c:33)
==28357==

(095) void handlePath(char *input) {
(096)     if(DEBUG_ON) { printf("%s%s\n", "DEBUG_HANDLEPATH: ", input); }
(097)
(098)     char *inputCopy = NULL;
(099)     inputCopy = (char *)malloc((strlen(input)+1)*sizeof(char));
(100)
(101)     if(inputCopy==NULL) {
(102)         die("malloc() failed in handlePath()");
(103)     }
(104)     strncpy(inputCopy, input, strlen(input)*sizeof(char));
(105)     printf("%s\n", inputCopy);
(106)     free(inputCopy);
(107)     return;
(108) }

Linie 96 druckt die parameter "char *input" just fine (DEBUG_ON==1), aber die Linie 105 spuckt valgrind-Fehler (nicht drucken, nur feiner in der Konsole). "char *input" stammt aus einer getline (), der nach einer Zeile, und in dieser Funktion so etwas wie "Pfad /test/path" ohne Anführungszeichen. Ich kann drucken und Bearbeiten es ganz gut in vorangegangenen Funktionen. Was nicht initialisierte über "char *inputCopy"? Irgendwelche Ideen? Vielen Dank im Voraus!

InformationsquelleAutor der Frage yavoh | 2010-01-30

Schreibe einen Kommentar