Conditional jump or move depends on uninitialised value(s)

This is a discussion on Conditional jump or move depends on uninitialised value(s) within the C Programming forums, part of the General Programming Boards category; Hello... I don't know why, but valgrind keeps giving me this error: Code: ==2187== Conditional jump or move depends on ...

  1. #1
    Registered User Tiago's Avatar
    Join Date
    Oct 2009
    Location
    Lisbon, Portugal
    Posts
    28

    Conditional jump or move depends on uninitialised value(s)

    Hello...

    I don't know why, but valgrind keeps giving me this error:

    Code:
    	==2187== Conditional jump or move depends on uninitialised value(s)
    	==2187==    at 0x40A550B: vfprintf (vfprintf.c:1614)
    	==2187==    by 0x40C370B: vsprintf (iovsprintf.c:43)
    	==2187==    by 0x40AC1DA: sprintf (sprintf.c:34)
    	==2187==    by 0x80492DA: OpenFiles (files.c:41)
    	==2187==    by 0x80487F7: main (main.c:30)
    	==2187==  Uninitialised value was created by a heap allocation
    	==2187==    at 0x4024F20: malloc (vg_replace_malloc.c:236)
    	==2187==    by 0x804925E: OpenFiles (files.c:34)
    	==2187==    by 0x80487F7: main (main.c:30)
    Function OpenFiles:

    Code:
    Files_str *OpenFiles(char *arg){        /* this function changes the file extension  */
    	char *fpInName, *fpOutName;
    	
    	fpInName = arg; /* arg is something like this: "blablablablabla.txt" */
    	
    	fpOutName = (char *) malloc(strlen(fpInName) + 1); /* LINE 34 */
    	if(fpOutName == ((char *) NULL)){
    		fprintf(stderr, "ERROR!!! Failed memory allocation for fpOutName\n");
    		exit(1);
    	}
    	
    	strncpy(fpOutName, fpInName, strlen(fpInName) - 4);
    	sprintf(fpOutName, "%s.exe", fpOutName); /* LINE 41 */
    	 /* now fpOutName is something like this: "blablablablabla.exe" */
    	 /* and fpInName is something like this: "blablablablabla.txt" */
    
    	.
    	.
    	.
    	.
    I guess I'm doing something wrong... anyway, the program runs fine...
    Last edited by Tiago; 05-23-2010 at 05:49 AM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,646
    Your usage of strncpy() will not terminate the string with a null terminator. That, plus the fact you are writing fpOutName to itself probably explains the warnings associated with sprintf().

    I'm not 100% sure on the concern with malloc(), but suspect it is a side effect of your strncpy()/sprintf() problems - it's possible that code is accessing values at some memory location before that memory is initialised.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,834
    valgrind is complaining about the lack of the \0 you didn't add with the strncpy.

    That is happens to find a \0 in the correct place is pure luck (it seems to work).
    After some more malloc/free calls, you might find yourself with a block containing old junk from previous use in your program (memory allocated for the first time is probably filled with zeros, but even this can't be guaranteed).

    > sprintf(fpOutName, "%s.exe", fpOutName); /* LINE 41 */
    Overlapping input and output buffers are undefined for nearly all of the C library calls. One notable exception being memmove().

    Use strncpy() to remove the last 4 characters, then make sure there is a \0.
    Using strcat() to append ".exe" would be a lot safer than using sprintf()
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User Tiago's Avatar
    Join Date
    Oct 2009
    Location
    Lisbon, Portugal
    Posts
    28
    Ok, thank you... problem solved...

    Code:
    void OpenFiles(char *arg){
    
    	char *fpInName, *fpOutName;
    
    	fpInName = arg;
    	
    	fpOutName = (char *) malloc(strlen(fpInName) + 1);
    	if(fpOutName == ((char *) NULL)){
    		fprintf(stderr, "ERROR!!! Failed memory allocation for fpOutName\n");
    		exit(1);
    	}
    	
    	strncpy(fpOutName, fpInName, strlen(fpInName) - 4);
    	fpOutName[strlen(fpInName) - 4] = '\0';
    	
    	strcat(fpOutName, ".exe");
    
    	.
    	.
    	.
    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  2. Forced moves trouble!!
    By Zishaan in forum Game Programming
    Replies: 0
    Last Post: 03-27-2007, 07:57 PM
  3. Replies: 2
    Last Post: 03-24-2006, 08:36 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Formatting Output
    By Aakash Datt in forum C++ Programming
    Replies: 2
    Last Post: 05-16-2003, 09:20 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21