Stack smashing detected (Aborted Core Dump)

My codes is suppose to replicate the strncat() function.
The codes give the desired output, but it always ends up with the stack smashing detected error. I’d like to know why and how to fix it.
What am I doing wrongly or not doing at all?

main.h

#ifndef MAIN_H
#define MAIN_H

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

int _putchar(char c);
char *_strcat(char *dest, char *src);
char *_strncat(char *dest, char *src, int n);

#endif

1-main.c

#include "main.h"
#include <stdio.h>

/**
 * main - check the code
 *
 * Return: Always 0.
 */

int main(void)
{
        char s1[98] = "Hello ";
        char s2[] = "World!\n";
        char *ptr;

        printf("%s\n", s1);
        printf("%s", s2);
        ptr = _strncat(s1, s2, 1);
        printf("%s\n", s1);
        printf("%s", s2);
        printf("%s\n", ptr);
        ptr = _strncat(s1, s2, 1024);
        printf("%s", s1);
        printf("%s", s2);
        printf("%s", ptr);
        return (0);
}

And this is the code for the custom version of the strncat() function
1-strncat.c

#include "main.h"

/**
 * _strncat - Concatenates strings
 * @dest: destination string
 * @src: source string
 * @n: Max bytes
 * Return: dest
 */

char *_strncat(char *dest, char *src, int n)
{
        int srclen = 0;
        int destlen = 0;
        int i = 0;
        int j = 0;

        while (src[srclen] != '\0')
        {
                srclen++;
        }
        while (dest[destlen] != '\0')
        {
                destlen++;
        }
        for (i = destlen; (i < (destlen + n) && j < n); i++)
        {
                dest[i] = src[j];
                j++;
        }
        dest[destlen + n] = '\0';
        return (dest);
}

enter image description here

>Solution :

Your function (correctly) determines the value of srclen but then never uses it … but it should use it.

That value should be the ‘limit’ for the j variable in the for loop, because that j is used as the index in the src string. Your use of j < n is thus wrong – the n limit is taken care of by the other test, i < (destlen + n).

Also, the dest[destlen + n] = '\0'; line is wrong, because it does not take into consideration how many characters have actually been copied. That line, occurring immediately after the for loop has finished, can be simply dest[i] = '\0';, using what will be the correct i value.

Here is a working version of your function:

char* _strncat(char* dest, const char* src, int n)
{
    int srclen = 0;
    int destlen = 0;
    int i = 0;
    int j = 0;

    while (src[srclen] != '\0') {
        srclen++;
    }
    while (dest[destlen] != '\0') {
        destlen++;
    }

    for (i = destlen; i < (destlen + n) && j < srclen; i++) {
        dest[i] = src[j];
        j++;
    }
    dest[i] = '\0'; // At loop exit, "i" will point to where the nul needs to be added.

    return dest;
}

Note that I’ve also added the const qualifier to the src parameter; this is not actually necessary but keeps your function’s signature consistent with the standard strncat.

Leave a Reply