valgrind (Conditional jump or move depends on uninitialised value(s))


 
Thread Tools Search this Thread
Top Forums Programming valgrind (Conditional jump or move depends on uninitialised value(s))
# 1  
Old 05-28-2009
valgrind (Conditional jump or move depends on uninitialised value(s))

Hi everybody,

I wrote a small subroutine 'GetStringDelim()' to return a substring from a string enclosed by a string 'Limit1' and a char 'Limit2'. It works perfectly and I don't get an error message together with valgrind, if I set the 3rd parameter in GetStringDelim() to NULL (see below).

Code:
main()
{
   ...
   SubString = GetStringDelim(String,"small",NULL);
  ...   
}

But if I replace the third parameter with any character the code works as it is supposed to be, but I get the following message from valgrind:
Code:
==30534== 1 errors in context 1 of 1:
==30534== Conditional jump or move depends on uninitialised value(s)
==30534==    at 0x4023CA7: strlen (mc_replace_strmem.c:242)
==30534==    by 0x407D811: vfprintf (in /lib/tls/i686/cmov/libc-2.7.so)
==30534==    by 0x4083362: printf (in /lib/tls/i686/cmov/libc-2.7.so)
==30534==    by 0x8048546: main (stringLTD.c:17)
==30534==  Uninitialised value was created by a heap allocation
==30534==    at 0x4022D78: malloc (vg_replace_malloc.c:207)
==30534==    by 0x8048636: GetStringDelim (stringLTD.c:60)
==30534==    by 0x8048530: main (stringLTD.c:15)

Does anyone has an idea what do I have to fix? Thanks!

Here is the complete code:

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

char *GetStringDelim(char *input, char *startstrg, char *limit);
char *GetString(char *input, char *string);

main()
{
   char *String="This is just a small example-string";
   char *SubString=NULL;

   printf("String %s\n",String);
   SubString = GetStringDelim(String,"small","-");
   printf("Substring:%s\n",SubString);
   
   free(SubString);
   
   return 0;
}

char *GetStringDelim(char *Input, char *Limit1, char *Limit2)
{
  int     Size = 0;
  char     *Start = NULL;
  char     *End = NULL;
  char     *String = NULL;
  int     i = 0;
  
  /* check input Limit1 */
  if(Limit1 == NULL)
  {
    Start = Input;
  }
  else
  {
    Start = GetString(Input,Limit1)+strlen(Limit1);
  }

  /* check input Limit2 */
  if(Limit2 == NULL)
  {
    String = (char*) malloc((strlen(Start)+1) * sizeof(char)); /* one more for NULL-char */
    strcpy(String,Start);

    return String;
  }
  else
    End    = strpbrk(Start,Limit2);
     
  /* get size of output string */
  Size = End - Start;
  
  if(Size <= 0)
    return NULL;
  
  /* copy string */
  String = (char*) malloc((Size+1) * sizeof(char)); /* one more for NULL-char */
  strncpy(String,Start,Size);

  return String;
}



char *GetString(char *input, char *string)
{
  char *in_incr=NULL, *str_incr=NULL;

  if ( !*string )
     return input;
  
  for (; *input; ++input ) 
  {
     if ( toupper(*input) == toupper(*string) ) 
     {
           for ( in_incr =input, str_incr = string; *in_incr && *str_incr; ++in_incr, ++str_incr )
           {
              if ( toupper(*in_incr) != toupper(*str_incr) )
         {
            break;
         }
      }
      
      if ( !*str_incr ) /* if current position is opposite of Nullstring (means 1) return start adress */
      {           
           return input; /* return the start of the match */
      }
     }
  }

  return NULL;
}

# 2  
Old 05-28-2009
Why use valgrind for something that works correctly...so leave well enough alone. Only thing maybe to split up variable initializers like...
Code:
char *String="This is just a small example-string";

to something like
Code:
char s[]="This is just a small example-string";
char *String=s;

# 3  
Old 05-28-2009
And #include <ctype.h> for toupper() calls.
# 4  
Old 06-02-2009
Hey guys,

thanks for your help, but your proposals didn't resolve this issue. However, I decided to keep it like it is.

The point why I wanted solve the error messegae, which was provided by valgrind, is that the present subroutines are part of bigger code. That code gave me some strange results. I could resolve those bugs by use of valgrind. So I thought it might be a good idea to fix all problems in order to avoid future bugs.

By the way, more ideas are welcome... :-D
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Using Valgrind with already running process

I have a process(c program) which runs as daemon and is causing memory leak. Is there any way to detect memory leak in this already running process? Just like we attach gdb to a live process to debug it, can I use valgrind to detect memory leak in such live process? When I tried using valgrind... (3 Replies)
Discussion started by: rupeshkp728
3 Replies

2. Shell Programming and Scripting

While loop depends on variable

The variable N is set upon start of script so while loop runs and keeps running because N is not being updated when loop is running: N becomes less than 9 but it still runs (should be "done").... Any ideas? Thanks! N=`wc -l /home/count.txt | awk '{print $1}'` while test "$N" -gt "10"... (10 Replies)
Discussion started by: holyearth
10 Replies

3. Ubuntu

valgrind

Hello, I want to install and run a tool called Daikon on my Ubuntu(latest version) While compiling I got this error--------> checking the GLIBC_VERSION version... unsupported version configure: error: Valgrind requires glibc version 2.2 - 2.11 make: *** Error 1 make: Leaving directory... (3 Replies)
Discussion started by: paramad
3 Replies

4. Programming

Valgrind Error : Conditional jump or move depends on uninitialised value(s)

Hi Friends, I am using valgrind, to check errors, there are no errors in my code but this the output put i get, i need to suppress these errors, please help me do so... Conditional jump or move depends on uninitialised value(s) ==2350== at 0x400AF0F: (within /lib/ld-2.11.1.so)... (0 Replies)
Discussion started by: niranjanvg
0 Replies

5. Programming

Valgrind error

I get this Valgrind error while using malloc but if I use calloc then there is no error. I allocate 8 bytes for the string inside sprintf, 12 for the ip and 1 for the string terminator. This totals 21, so why is it talking about 20 bytes and 18 bytes ? Partial code: char ip =... (3 Replies)
Discussion started by: cyler
3 Replies

6. UNIX for Dummies Questions & Answers

valgrind Conditional jump or move depends on uninitialised value(s)

Hi, I know similar questions appeared here already, still i didnt find answer to my problem yet. Im getting the following message from valgrind (version 2): ==15414== 1 errors in context 1 of 6: ==15414== Conditional jump or move depends on uninitialised value(s) ==15414== at... (2 Replies)
Discussion started by: evasz
2 Replies

7. AIX

Can Valgrind work well on AIX?

As Valgrind announced, 3.3.0 and 3.3.1 version can support AIX 5.3. But I met a block issue when I used Valgrind on AIX After installing Valgrind3.3.1 successfully on AIX5.3, I typed the following command of Valgrind: valgrind -d --tool=memcheck ls Then, the following result from Valgrind is... (0 Replies)
Discussion started by: adasong
0 Replies

8. Shell Programming and Scripting

Depends on Machine?

I am trying to learn PERL programming. I copied this example from a book just to try it on my machine. It worked out fine but when I tried it on another machine, an error appears: syntax error near unexpected token ';'. I tried it on a third machine but a new error appears: command not found. ... (4 Replies)
Discussion started by: Slick
4 Replies

9. Shell Programming and Scripting

Please my job depends on this ....

I need to write a script that will move files from a direcory to another directory, but only the files that are owned by a specific owner. e.g. if seven files in a dir are owner by a user called 'budd' then move to a direcory called \budd. If not then leave alone. Many Thanks (2 Replies)
Discussion started by: Budd
2 Replies
Login or Register to Ask a Question