[Sussex] Re: C programming help - again!

Captain Redbeard hairy.one at virgin.net
Tue May 10 20:26:19 UTC 2005


Thomas Adam wrote:

> That would be """fixed""" as opposed to just 'Fixed' --
> strncpy() is the,function you should use -- it's much
> better than the much older,stncpy(), doesn't allow for
> buffer-overflows on non-terminated strings,as easily, and
> whatnot.

OK, just one small problem.  Originally the corrected 
program read like this:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>


int main()
{
   struct TestStruct
   {
     int X;
     int Y;

     char *CharString;
   } *TestStructPtr;

   TestStructPtr = malloc (sizeof (struct TestStruct));
   TestStructPtr->CharStr = malloc (20);

   TestStruct->X = 3;
   TestStruct->Y = 4;
   strcpy (TestStructPtr->CharString, "Hello World!\n\n\n");

   printf ("TestStructPtr = (%i, %i).\n\n", 
TestStructPtr->X, TestStructPtr->Y);
   printf ("CharString = %s.", TestStructPtr->CharString);

   free (TestStructPtr->CharString);
   free (TestStructPtr);

   return 0;
}

This worked fine, gcc was happy with it, Valgrind found no 
errors and it ran as expected.  I then changed the 
"strcpy()" line to:

strncpy (TestStructPtr->CharString, "Hello World!\n\n\n", 13);

Now it still compiles and runs OK but when I checked it with 
Valgrind I got the following error message:

Conditional jump or move depends on uninitialised value(s)
    at 0x........: strlen (mac_replace_strmem.c:189)
    by 0x........: vfprintf (in /lib/libc-2.3.4.so)
    by 0x........: printf (in /lib/libc-2.3.4.so)
    by 0x........: main (struct1.c:25)

What does this mean exactly?  Is there a problem with the 
way I call strncpy?  I've been experimenting with this 
program for the past hour or so and can't find any other 
possible source of error but I also don't see what's wrong 
with the call.  So, WTF?



-- 
Captain Redbeard



======================================================
Insomnia - it's not a condition, it's a mentality.
Registered Linux user: 388693.
Registered Linux machines: 289172, 289173.
v2sw5+8C+JGhw5ln4/5pr6/7Ock1ma6/8l6+8Sw3+2e4t2b9Hen4+6
g5+3RHaIr9 Hackerkey.com
======================================================





More information about the Sussex mailing list