-1

I have an error at the last line, in nullString, a function setting all the string to '\0' with a simple for()

void function ( unsigned char inputArray[], size_t inputSize )
{
    size_t cellSize;
    if (inputSize <= 256)
        cellSize = 1;
    else
        cellSize = ceil(inputSize / 2 / 256) + 1;


    // Sub Box
    unsigned char subBox[255];
    for (size_t line = 0; line < 255; line++)
        subBox[line] = 0;

    generate_SubBox(subBox, key);
    // Sub Box


    // Sub Box reverse
    unsigned char subBox_Inverse[255];
    for (size_t line = 0; line < 255; line++)
        subBox_Inverse[line] = 0;

    generate_SubBox_Inverse(subBox_Inverse, subBox, key);
    // Sub Box reverse        

    unsigned char* inputArray2 = NULL;
    inputArray2 = malloc(sizeof(unsigned char)* inputSize / 2);
    verifyMalloc(inputArray2);
    nullString(inputArray2, inputSize / 2);

    unsigned char string_temp[3] = { 0 };
    size_t w = 0;
    for (size_t i = 0; i < inputSize / 2; i++)
    {
        string_temp[0] = inputArray[w];
        string_temp[1] = inputArray[w + 1];

        inputArray2[i] = strtoll(string_temp, NULL, 16);

        w += 2;
    }
}

I tried neutralizing line per line all instructions coming before nullString() by commenting them but it doesn't change anything.

If I neutralize nullString, the error comes after, at

inputArray2[i] = strtoll(...)

Hope you've got the answer :)

Thanks in advance !

EDIT: Here is nullString:

void nullString(unsigned char input[], size_t length)
{
    for (size_t x = 0; x < length; x++)
        input[x] = '\0';
}

I commented all the instructions before nullString, the error is still there.

I also verified variables and they all look like good

EDIT 2: verifyMalloc:

void verifyMalloc(int* pointer)
{
    if (pointer == NULL)
    {
        perror("Erreur");

        Sleep(15000);
        exit(0);
    }
}
Tom Clabault
  • 481
  • 4
  • 18

2 Answers2

3

Everything we're seeing is seriously hinting at you forgetting to #include <stdlib.h> (and ignoring the warnings resulting from that).

This is what might happens when you use malloc() without including stdlib.h in the same file:

  • the compiler consider the malloc() function to be declared implicitly, which means it is assuming that it's return types is int (instead of *void).
  • This might work when sizeof (int) is the same as sizeof (*void). But when int is 32-bits while pointers are 64-bits then the address returned by malloc() might lose half of it's bits and point to an invalid address.
SiggiSv
  • 1,219
  • 1
  • 10
  • 20
0

Try using

void bzero(void *s, size_t n); or
void *memset(void *s, int c, size_t n);

instead of your nullString() and for()something[x]=0 loops.

Then, this does not make all of the array zeroed:

unsigned char string_temp[3] = { 0 };

This makes

string[0] = 0;
string[1] = god_knows;
string[2] = god_knows_not;

so either - unsigned char string_temp[3] = {0,0,0}; 
or bzero(string_temp,3);

Consequently, when you do this:

string_temp[0] = inputArray[w];
string_temp[1] = inputArray[w + 1];

inputArray2[i] = strtoll(string_temp, NULL, 16);

strtoll() will be guessing when to stop. No guarantee this would be at string_temp[2].

Then, this should be enough:

unsigned char* inputArray2 = malloc(sizeof(unsigned char) * inputSize / 2);

inputArray2 will be NULL if malloc failed, or a valid pointer if it succeeded.

You may want to check your inputSize / this_and_that arithmetics. Does it really deliver what you expect? You might be surprised by division result of integer operands.

This also looks suspicious:

inputArray2[i] = strtoll(string_temp, NULL, 16);

strtoll returns longlong integer but your inputArray2 is of unsigned char type. So you are trying to store 8 bytes (sizeof longlong = 8) and you reserved place only for one (sizeof char = 1)

Redeclare your inputArray2 as long long

long long *inputArray2 = malloc(sizeof(long long) * inputSize /2 );

And try this with memset():

size_t size = sizeof(long long) * inputSize/2; 
//Do you really need long long? You are storing max three digits. uint_8 will be enough

long long* inputArray2 = malloc(size);
memset(inputArray2, 0, size);
Sokre
  • 114
  • 6
  • inputSize / 2 gives me the exact value i'm excepting, no problem at that level so I left inputArray2 = malloc(...) as it was Thanks for string_temp[3] = { 0 }, it's now string_temp[3] = { 0, 0, 0 } Memset ( beacause i'm under Windows 10 and Visual Studio ) does not help, same error but when executing memset. I also checked the malloc with if(... == NULL), no problem, the malloc is good... – Tom Clabault Jun 12 '17 at 19:53
  • Where do you get the error now? In memset(), which you are now using instead of nullString(); Or later, when you try inputArray2[i] = strtoll(); – Sokre Jun 12 '17 at 20:01
  • The error is in memset(), i don't use nullString anymore. If I comment memset(), then the error comes at "inputArray2[i] = strtoll(string_temp, NULL, 16);" – Tom Clabault Jun 12 '17 at 20:05
  • 1
    "Then, this does not make all of the array zeroed: `unsigned char string_temp[3] = { 0 };`" Yes, it does. See: https://stackoverflow.com/a/2589751/4766261 – SiggiSv Jun 12 '17 at 21:27