How to properly set size of int array in C without using malloc or similars in order to avoid a SIGABORT (signal -6)

109 Views Asked by At

I have to create a given function that allows me to convert a given string (homemade @atoi) of numbers, to a given base (2,8,16, etc) and returns the result: assignment

Problem is that I thought my function was okay but then it gets SIGABORTs everywhere, because indeed, I cant seem to initialize the INT[] with a valid size.

This is my code

#include <stdio.h>

int ft_atoi(char *str)
{
    int i;
    int sign;
    int num;

    i = -1;
    sign = 1;
    num = 0;
    while (str[++i] < '0' || str[i] > '9')
        if (str[i] == '-')
            sign *= -1;
    while (str[i] >= '0' && str[i] <= '9')
        num = num * 10 + str[i++] - '0';
    return (num * sign);
}

int check_base(char *base)
{
    int i;
    int z;

    i = -1;
    z = 0;
    if (base[0] == '\0' || base[1] == '\0')
        return (0);
    while (base[++i])
    {
        z = i + 1;
        if (base[i] == '+' || base[i] == '-')
            return (0);
        if (base[i] < 32 || base[i] > 126)
            return (0);
        while (base[z++])
        {
            if (base[i] == base[z])
                return (0);
        }
    }
    return (1);
}

int ft_putnbr_base(int nbr, char *base)
{
    int size_base;
    int nbr_final[(sizeof(int))]; *// I think that this is the troublemaker*
    int i;
    int final;

    i = 0;
    final = 0;
    size_base = 0;
    if (check_base(base))
    {
        if (nbr < 0)
            nbr = -nbr;
        while (base[size_base])
            size_base++;
        while (nbr)
        {
            nbr_final[i++] = nbr % size_base;
            nbr = nbr / size_base;
        }
        while (--i >= 0)
            final = final * 10 + nbr_final[i];
    }
    return (final);
}

int ft_atoi_base(char *str, char *base)
{
    return (ft_putnbr_base(ft_atoi(str), base));
}

int main(void)
{
    printf("%d", ft_atoi_base("10", "01"));  *// <== Here is where trouble begins, as soon as **str** starts to grow it fails*
}

I did try to use valgrind and gdb with little to no success (since I'm not assinging memory willingly?)

1

There are 1 best solutions below

2
NoDakker On

I copied your code, cleaned up a couple of comments that wouldn't let me compile the code, compiled the code, and then ran the example. The sample code as is did run without any program dump. However, when I increased the size of the value to the following:

    printf("%d", ft_atoi_base("10101010", "01"));

I got a stack smashing error, meaning that some statement was filling in an array past its bounds.

Reviewing the code I saw that you marked a line of code as being suspect.

    int nbr_final[(sizeof(int))]; // I think that this is the troublemaker*

And, your suspicions would be correct.

I added a couple of printf statements to act as a terminal level debugger to follow the processing of the integer value that was derived from the string in the ft_putnumber_base function.

int ft_putnbr_base(int nbr, char *base)
{
    int size_base;
    printf("Size of integer: %ld\n", sizeof(int));          /* To note the actual size value of an int */
    int nbr_final[(sizeof(int))]; // I think that this is the troublemaker*
    int i;
    int final;

    i = 0;
    final = 0;
    size_base = 0;
    if (check_base(base))
    {
        if (nbr < 0)
            nbr = -nbr;
        while (base[size_base])
            size_base++;
        while (nbr)
        {
            printf("nbr is: %d and i is: %d\n", nbr, i);    /* To track the stack smashing */
            nbr_final[i++] = nbr % size_base;
            nbr = nbr / size_base;
        }
        while (--i >= 0)
            final = final * 10 + nbr_final[i];
    }
    return (final);
}

With that, I reran the code with the larger integer number and produced this terminal output.

@Dev:~/C_Programs/Console/Homemade/bin/Release$ ./Homemade 
Size of integer: 4
nbr is: 10101010 and i is: 0
nbr is: 5050505 and i is: 1
nbr is: 2525252 and i is: 2
nbr is: 1262626 and i is: 3
nbr is: 631313 and i is: 4
nbr is: 315656 and i is: 5
nbr is: 157828 and i is: 6
nbr is: 78914 and i is: 7
nbr is: 39457 and i is: 8
nbr is: 19728 and i is: 9
nbr is: 9864 and i is: 10
nbr is: 4932 and i is: 11
nbr is: 2466 and i is: 12
nbr is: 1233 and i is: 13
nbr is: 616 and i is: 14
nbr is: 308 and i is: 15
nbr is: 154 and i is: 16
nbr is: 77 and i is: 17
nbr is: 38 and i is: 18
nbr is: 19 and i is: 19
nbr is: 9 and i is: 20
nbr is: 4 and i is: 21
nbr is: 2 and i is: 22
nbr is: 1 and i is: 23
*** stack smashing detected ***: terminated
Aborted (core dumped)

My guess is that there might be more issues with this code, but that was the primary one that stood out. Concentrate there first and see if you can progress.