Amazon Interview Question for Software Engineer / Developers






Comment hidden because of low score. Click to expand.
1
of 1 vote

Well, to understand what is happening in this case, one needs to understand the way sub routine calls are handled by the compiler which in turn is executed by the OS using a stack to store state and pass parameters.

So when you make a call to a func with the following declaration:

int myFunc(int val1, char val2)

what is really happening is that (assuming this is a regular function call and not a fastcall. In the later everything happens over registers)
a. The input parameters - val1 (4 bytes) and val2 (1 byte) are pushed on to the stack. The order and size will depend on the language and compiler.
b. The return address (of the current processor pointer) is pushed onto the stack
c. The local variables get allocated on top.

So visually, the process stack looks like this:

| local variable n |
| local variable n-1 |
| ... |
| local variable 1 |

| RETURN ADDRESS |

| parameter n |
| parameter n-1 |
| ... |
| parameter 1 |


After executing the function there is an instruction to return back by poping out the RETURN ADDRESS on the stack and returning to that location for the next instruction to execute.

Now, coming back to this program, the Buffer Overflow (BO) will first exceed the 50 units of storage on the stack for the local variable (array in this case), then overwrite the 'return address' and go ahead. Finally, when the program finishes it tries to return but to address '51', which is meaningless and most likely contains no valid instructions or in some cases an instruction that tries to reference an invalid memory location i.e. a virtual address that is not backed by content on physical memory. The latter can result in a segmentation fault (which in the windows world is error - 'Access violation')

- Zukova January 01, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

When I tried the program, I did not see the program crash. What I have seen is that the program just runs into an endless loop.

The reason why there is an endless loop is that, as I guess, the buffer overflow after 50 elements just erased(or say overwrites the value of local variable i), then i becomes 0, so it is able to start the for loop from 0... and this goes on again and again...

- ultraviolet January 30, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

You can even put a 'printf' before 'buff[i]' to trace exactly how the value of 'i' is changed. You should be able to see, 0-51 1-51 1-51... loops like this.

- ultraviolet January 30, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I tried with a printf inside for...
it prints upto 100 and throws segmenation fault after that...

instead of 100 I changed it to 250 and it behaves same..
prints upto 250 and throws segmentation fault...

- Manley March 03, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In java there would through an ArrayIndexOutOfBoundsException when i=50. In C I believe it will keep running as long as there is a valid memory address there, so it's kind of unpredictable.

- Anonymous March 06, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hey guys, you're learning how to hack!

What you are doing is overwriting the value at the address of buff[i]. Thus, at the point i=51 & >, you are doing nothing wrong and so your program doesn't crash.

Ex:

&buff = 0x00 (** FOR EXAMPLE **)
so &buff[51] = 0x33

and so, buff[51] = 0 simply does that.

OK, so easy right? But why not crash, well that's simply. You're program is allowed to modify that address. If the OS were to restrict access to the address for your process, then BOOM, crash. If another thread in your program were using that address and you're putting a 0 in that address causes problems...then BOOM, crash. (I use "BOOM" and "crash" loosely).

Why is this hacking, because this is how your program gains access to other parts of the PC (if you will). How or why is this significant for hacking...well, if you're truly interested, you'll figure it out....

- Fusedentropy March 07, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

as mentioned by ultraviolet, buff[51] is nothing but the 'i', hence i gets reset to zero.

- Anonymous November 08, 2007 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Well,
char * s = "hello";
s[1] = 'a'; <-----------CRASH!!

Usually, strings like s="hello" are placed in the read-only section. So when you try to assign a value to s[1], it "booms".

- bawa August 09, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

char * s = "hello";
s[1] = 'a'; <-----------Doesnt CRASH in C

- Gir November 08, 2007 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

for me running this prog on ubuntu 10.04...says
*** stack smashing detected ***: ./a.out terminated

- Anonymous December 17, 2010 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More