0

I was inspecting the following C code and my knowledge is that when you increment a pointer (by += 2 etc) it will increment the pointer's address by 2 (in this example) * the size of the pointer type. The following code is from a console application which works but this knowledge seems not to apply here?

void some_function(data_type_for_something *c, unsigned long *data, int blocks)
{
    unsigned long *d;
    int i;

    d = data;
    for (i = 0; i < blocks; i++)
    {
        change_values_at_d_and_d_plus_one_to_new_ones(c, d, d+1);
        d += 2;
    }
}

The blocks parameter is always the size of the data buffer (the amount of longs inside the buffer), as being called here:

 some_function(some_value, (void *)data, datalen / 8);

Where the data is a buffer of unsigned chars. If you take a took at the some_function you can see it will access memory outside of the buffer. What could be the reason for this?

Im trying to make this code in java:

public static long[] some_function(the_data_type bc, long[] data, int blocks) {
        long[] ret = new long[data.length];
        int index = 0;
        for(int i = 0;i<blocks; i++) {
            Pair<Long,Long> r = function_with_long_name(bc, data[index], data[index+1]);
            ret[index]=r.getFirst();
            ret[index+1]=r.getSecond();
            index+=2;
        }
        return ret;
    }

I get an array out of bounds exception (which makes sense) Please tell me if there is anything else I should include in the question.

Note, the unsigned longs can be just longs in java as the values will never be bigger than the largest long type which is positive.

Full code is https://www.di-mgt.com.au/blowfish.c.html if needed

  • 3
    Accessing an array element out of bounds in C is *undefined behaviour* and will not *necessarily* cause an *immediate* error. Possible duplicate: (especially the accepted answer): https://stackoverflow.com/q/15646973/10871073 – Adrian Mole Aug 29 '20 at 01:07
  • data[index] will throw after blocks/2 iterations since you increments index by 2 for each iteration.For the loop, try setting for loop condition to for(...;i < blocks/2 -1;) – printfmyname Aug 29 '20 at 01:12

1 Answers1

0

The two comments above are correct, you should make sure you understand them. I think the easiest way to correct this code is just to remove the extraneous variables and use the loop invariant directly.

Make sure to check all applicable bounds (note the && I added).

public static long[] some_function(the_data_type bc, long[] data, int blocks) {
        long[] ret = new long[data.length];
        for (int index = 0; index<blocks && index < ret.length-1; index += 2) {
            Pair<Long,Long> r = function_with_long_name(bc, data[index], data[index+1]);
            ret[index]  = r.getFirst();
            ret[index+1] = r.getSecond();
        }
        return ret;
    }
markspace
  • 10,621
  • 3
  • 25
  • 39
  • Thats helped a lot. I have one more slightly off-topic question. I want to include this library in a dll (the original C code). When I export the functions in it, the names of the functions arent just the name of the function. I use dllexportviewer to see all functions when I export and the names of the functions have the full declaration like 'unsigned char * __cdecl my_function' when I just want it to be exported like other dlls as just 'my_function' I use a macro '#define DLL_EXPORT __declspec(dllexport)' before each function definition. How do I do this? – lucas.ss.05 Aug 29 '20 at 02:26