1

My C code below is trying to remove the trailing white space from a string pointer. while testing the code I am getting a "Conditional jump or move depends on the uninitialized value(s)" a line 3. Since I am trying to read the length of a string pointer, do I need to initialize it?

void trim(char *s)
{
    int i = strlen(s) - 1;
 
    while (i > 0)
    {
        if (s[i] == ' ' || s[i] == '\t')
            i--;
        else /* where you see very first from the last nonwhite space*/
            break;
    }
    s[i + 1] = '\0';

}

function trim is called in another function which is reading input from commandline

char *UserInput()
{
    char *input = (char *)malloc(120);

    char command[120];
    int len = read(STDIN_FILENO, command, 120 - 1);
    int i;
    for (i = 0; i < len; i++)
    {
        if (command[i] == '\n')
        {
            break;
        }
        input[i] = command[i];
    }
    trim(input);
    return input;
}

Ada_lovelace
  • 637
  • 2
  • 6
  • 18
  • 3
    The pointer has to point to a valid null-terminated string. Show how you're calling the function. – Barmar Jan 27 '23 at 02:03
  • Code fails to trim `" "` to `""`. – chux - Reinstate Monica Jan 27 '23 at 02:07
  • 2
    @Ada `strlen(s)` is not valid as `s` does not point to a _string_. A _string_ has a _null character_. – chux - Reinstate Monica Jan 27 '23 at 02:12
  • that's the acceptable size of the CLI string, declared and initialized in an another header file. – Ada_lovelace Jan 27 '23 at 02:15
  • 1
    Note: `char *input = (char *)malloc(120);` makes more sense as `char *input = malloc(len + 1);` _after_ the `read()` call. – chux - Reinstate Monica Jan 27 '23 at 02:26
  • `read` is a low-level unix function. It's not part of the standard C language. If you're a beginner, you shouldn't be using the `read` function. One problem with it is that It certainly does not put a null terminator at the end of the characters that it reads. Use `fgets` to read a line of input, and [read this answer](https://stackoverflow.com/a/28462221/3386109) to see how to correctly replace the newline character with a null terminator. – user3386109 Jan 27 '23 at 03:24
  • "Conditional jump or move depends on the uninitialized value(s)" Sounds misleading, could be a false positive. Which compiler and version are you using? On the other hand you don't seem to explicitly null terminate the string. You can try to swap malloc with calloc and see if the warning goes away. – Lundin Jan 27 '23 at 08:46

1 Answers1

3

Your copying of command to input character by character fails to terminate it with a NUL character, giving undefined behavior when you call strlen on the parameter passed to trim

Fix:

input[i] = '\0';
trim(input);
Govind Parmar
  • 20,656
  • 7
  • 53
  • 85
  • Hello Govind, I am very new to C, I thought that anything read from the command line gets a null-terminated character attached to the very end – Ada_lovelace Jan 27 '23 at 02:17
  • 3
    @Ada_lovelace It is, but your copy operation stops at the `\n` which is before the terminating `\0`, meaning `input` is not null terminated even if `command` is – Govind Parmar Jan 27 '23 at 02:19
  • 3
    A warning as you learn C, it was designed to be extremely simple, so sometimes omits features you would expect. Things as simple as a string copy function that works all the time. It's notorious as a "shoot yourself in the foot" language. – John Bayko Jan 27 '23 at 03:12