Reputation: 722
I have attended an algorithm competition where I have given a method to write an algorithm for a problem which is for language in C coding. The method was something like below:
int algorithm(char** grid, int gridSize, int* gridColSize)
{...}
If I give an input to server a value for a grid:
[]
Then I have received "AddressSanitizer ERROR heap-buffer-overflow"
when I printf() the value gridColSize like below:
printf("[%d]", gridColSize);
It prints:
[16]
If I print like below then the memory error shows at the editor's terminal
printf("[%d]", *gridColSize);
As I can't access the hidden code at server. How can I possibly check and deal with the input value "[]" for grid parameter.
For the following input where row and column values are valid it does not appears such case. if row is zero column may have valid value >= 1, so how can I check the gridColSize address is a valid memory and I can skip that input for returning a valid output?
I have seen many topics in internet about why address sanitizer issues happens but I have know how can I deal with this situation when I can't access main code.
Would someone help me out to solve it brilliantly?
Upvotes: 1
Views: 476
Reputation: 16540
this statement:
printf("[%d]", *gridColSize);
is requesting printing the value at address (per your question) 16.
Most likely address 16 is NOT a valid address for your program to use.
Upvotes: 0
Reputation: 1209
how can I check the gridColSize address is a valid memory and I can skip that input for returning a valid output?
You can't check if memory pointed by given non-NULL pointer can or can not be accessed.
Given API of algorithm
function you can:
grid
is NULL
.gridSize
is 0.gridColSize
is NULL
.Given that you checked 3 (gridColSize
was 16
) try checking gridSize
.
Upvotes: 1