Note that there are some explanatory texts on larger screens.

plurals
  1. POConsequences of this buffer overflow?
    primarykey
    data
    text
    <p>So here I believe I have a small buffer overflow problem I found when reviewing someone else's code. It immediately struck me as incorrect, and potentially dangerous, but admittedly I couldn't explain the ACTUAL consequences of this "mistake", if any.</p> <p>I had written up a test app to demonstrate the error, but found (to my dismay) that it seems to run correctly regardless of the overflow. I want to believe that this is just by chance, but wanted some feedback to determine if my thinking were wrong, or if there truly is a problem here that just isn't showing its head in my test app.</p> <p>The problem code (I think it is, anyway):</p> <pre><code>char* buffer = new char[strlen("This string is 27 char long" + 1)]; sprintf(buffer, "This string is 27 char long"); </code></pre> <p>Now, the reason this stood out to me and I want to flag it as a possible buffer overflow is because of the first <code>strlen</code>. Due to pointer arithmetic, the 'incorrect' placement of the <code>+ 1</code> will cause the <code>strlen</code> to return <code>26</code> instead of <code>27</code> (taking the length of "his string is 27 char long"). <code>sprintf</code>, I believe, then prints 27 char into the buffer and has caused a buffer overflow.</p> <p>Is that a correct assessment?</p> <p>I wrote a test app to demonstrate this for the person who's code I was looking at, and found that even in the debugger the string will print correctly. I also attempting putting other variables on the stack and heap before and after this code to see if I could affect neighboring areas of memory, but was still receiving correct output. I realize that my newly allocated heap memory might not be adjacent, which would explain the lack of useful overflow, but I just really wanted to confirm with others' opinions if this is in fact an issue.</p> <p>Since this is a pretty simple "question", it'd be nice if you could support your answer with some sort of reference as well. While I value and welcome your input, I'm not going to accept "yes it is" as the final answer. Thank you kindly in advance.</p> <p><br><br></p> <hr> <p><strong>Update:</strong> Many good answers with a lot of additional insight. Unfortunately, I can't accept them all. Thank you for sharing your knowledge and for being my 'second opinion'. I appreciate the help.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload