Home > Enterprise >  Buffer overflow? strncat() in C
Buffer overflow? strncat() in C

Time:10-28

I'm very new to C, and I want to make a simple program that gets an index of a string from the value, and once it has the index, it removes it from a string. It's causing a buffer overflow error? Pretty sure it's from strcat from searching online but I'm not sure. Any help is appreciated! Also please don't use * in your answer, because I don't know how they work, I will learn very soon. (if its required for your answer, explain how you use it in the code please)

heres the code:

#include <string.h>

int findIndex(char string[], char substr) {
    for (int index = 0; index < strlen(string); index  ) {
        if (string[index] == substr) {
            return index;
        }
    }
    return -1;
}

int main(void) {
    char howAreYou[9] = "howAreYou";
    char newString[9];

    int index = findIndex(howAreYou, 'o');
    
    for (int i = 0; i < 8; i  ) {
        if (i != index) {
            strncat(newString, &howAreYou[i], 1);
        }
    }

    printf("new string: %s", newString);
  return 0;
}

CodePudding user response:

Use char howAreYou[] = "howAreYou"; to allow for a terminating zero.
Use char newString[sizeof howAreYou] = ""; to have the correct size for the array and initialize the array so it is empty before concatenation.

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

int findIndex(char string[], char substr) {
    for (int index = 0; string[index]; index  ) {
        if (string[index] == substr) {
            return index;
        }
    }
    return -1;
}

int main(void) {
    char howAreYou[] = "howAreYou";
    char newString[sizeof howAreYou] = "";

    int index = findIndex(howAreYou, 'o');

    for (int i = 0; i < sizeof howAreYou; i  ) {
        if (i != index) {
            strncat(newString, &howAreYou[i], 1);
        }
    }

    printf("new string: %s\n", newString);
  return 0;
}

CodePudding user response:

A simple solution is to make a function that removes the desired char from your string. But make sure to add '\0' (null character) at the last of your string.

As when you'll remove one character from the string it's length should be decremented by one as all the characters after that position will be shifted to one position left so there isn't any need for last character

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

int findIndex(char string[], char substr)
{
   for (int index = 0; index < strlen(string); index  )
   {
      if (string[index] == substr)
      {
         return index;
      }
   }
   return -1;
}

// This function removes the desired character from the char array
void removeChar(char string[], int index)
{
   for (int i = index   1; i < strlen(string); i  )
   {
      string[i - 1] = string[i];
   }
   // Making the last character '\0' (null character) so that string can end here
   string[strlen(string) - 1] = '\0';
}

int main(void)
{
   char howAreYou[9] = "howAreYou";

   int index = findIndex(howAreYou, 'o');
   removeChar(howAreYou, index);

   printf("new string: %s", howAreYou);
   return 0;
}

Hope it helped! :)

CodePudding user response:

For starters you forgot to include the header <stdio.h>

It is better to define the array howAreYou like

char howAreYou[] = "howAreYou";

instead of

char howAreYou[9] = "howAreYou";

otherwise the array does not contain a string.

Correspondingly the array newString should be declared like

char newString[sizeof( howAreYou )];

The array newString does not not contain a string. So the call of the function strncat invokes undefined behavior.

Also in general you need to check the return value from the function findIndex.

As you are already using standard C string functions and want to remove only the first occurrence of the letter 'o' then it can be done simply using standard C function strchr.

For example

const char *p = strchr( howAreYou, 'o' );

if ( p == NULL )
{
    strcpy( newString, howAreYou );
}
else
{
    size_t n = p - howAreYou;
    strncpy( newString, howAreYou, n );
    strcpy( newString   n, p   1 );
}

Here is a demonstration program.

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

int main( void )
{
    char howAreYou[] = "howAreYou";
    char newString[sizeof( howAreYou )];

    const char *p = strchr( howAreYou, 'o' );

    if (p == NULL)
    {
        strcpy( newString, howAreYou );
    }
    else
    {
        size_t n = p - howAreYou;
        strncpy( newString, howAreYou, n );
        strcpy( newString   n, p   1 );
    }

    puts( newString );
}

The program output is

hwAreYou

If you want to use your own function findIndex then the program can look the following way

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

size_t findIndex( const char string[], char c )
{
    size_t i = 0;

    while (string[i] != '\0' && string[i] != c)   i;

    return string[i] == '\0' ? -1 : i;
}

int main( void )
{
    char howAreYou[] = "howAreYou";
    char newString[sizeof( howAreYou )];

    size_t n = findIndex( howAreYou, 'o' );

    if ( n == ( size_t )-1 )
    {
        strcpy( newString, howAreYou );
    }
    else
    {
        strncpy( newString, howAreYou, n );
        strcpy( newString   n, howAreYou   n   1 );
    }

    puts( newString );
}
  • Related