简体   繁体   中英

C line printed from file undefined behavior

I have made ac program that parses through the source code file of a language called rapid to extract certain data that I will need to document at work. the data extracted is saved to a csv file that is then formatted into an excel worksheet.

Everything is working except for this function that I have put below. In certain scenarios I was wanting to remove all of the spaces and tabs from a line read from a file so that I can store the statement as a string, in a struct attribute.

The program isn't crashing, but when I printf() the new line with the whitespace removed, some other characters get printed out to.

Example "cmd.exe" , "PowerShell\\v1.0\\Modules", "igh\\AppData\\LocaloYSφo¡"

If I do Printf("%s\\n", currentLine); It prints fine

When I use printf("%s\\n", removeWhiteSpace(currentLine)); I get the undefined behavior.

Here is the function

/******************************************************************
*   Takes a string as input, returns it without tabs or spaces
*   Used to put whole line into the additional commands
*   Attribute
******************************************************************/
static char* removeWhiteSpace(char* string)
{
    int i;
    int j;
    int len = strlen(string);
    char ch;
    char* result = malloc(sizeof(char)*len+1);

    memset(result, 0, sizeof(*result));

    j=0;
    for (i=0; i<len; i++)
    {
        ch = string[i];
        if ((ch != ' ') && (ch != '\t'))
            {
                result[j] = ch;
                j++;
            }
    }

    result[strlen(result)] = '\0';

    return result;
}

Also, I am using fgets() to get the line from the file, and the size for the buffer is at 1000.

The unwanted characters don't exist in the text file, at least not visible anyways.

Thank you for your time, and if you need the text file or the rest of the program I can provide it, but it is lengthy.

Also, I'm using codeblocks IDE using the GCC compiler, I have no errors or warnings when I compile.

memset(result, 0, sizeof(*result));

That is wrong. *result is the thing result points to. result is char * , so it points to a char , and the size of a char is 1. So that statement sets one char to zero. It does not set the entire block of allocated memory to zero.

As we will see, it is unneeded, so just delete that statement.

result[strlen(result)] = '\\0';

This statement is useless. strlen works by finding the first null (zero) character in an array. So strlen(result) would report where the first null character is. Then result[strlen(result)] = '\\0'; would set that character to zero. But it is already zero. So this statement can never accomplish anything. More than that, though, it does not work because the memset above failed to set the memory to zero, so there may be no null character inside the allocated memory to find. In that case, the behavior is not defined by the C standard.

However, there is no need to use strlen to find the end of the string. We know where the end of the string should be. The object j has been counting the characters written to result . So just delete this line too and use:

result[j] = '\0';

When I use printf("%s\\n", removeWhiteSpace(currentLine)); I get the undefined behavior.

That does not make any sense. “Undefined behavior” is not a thing. It is a lack of a thing. Saying something has “undefined behavior” means the C standard does not define what the behavior is. A program that has undefined behavior may print nothing, it may print a desired result, it may print an undesired result, it may print garbage characters, it may crash, and it may hang.

Saying a program produced undefined behavior does not tell anybody what happened. Instead, you should have written a specific description of the behavior of the program, such as “The program printed the expected text followed by unexpected characters.” A copy-and-paste of the exact input and the exact output would be good.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM