Tajuddin Khandaker
Tajuddin Khandaker

Reputation: 722

AddressSanitizer error while accessing value of a memory address though printf()

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

Answers (2)

user3629249
user3629249

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

Serikov
Serikov

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:

  1. check if grid is NULL.
  2. check if gridSize is 0.
  3. check if gridColSize is NULL.

Given that you checked 3 (gridColSize was 16) try checking gridSize.

Upvotes: 1

Related Questions