Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Few years ago I've got a call from my ex-colleague telling me about the problem he had to fix with my code, which was a router for credit card transactions.</p> <p>Card number prefix consists of 6-digit BIN (Bank Identification Number) and extra few digits that banks use at own discretion, e.g. bank has BIN for Visa Classic card 456789, and reserve 2 extra digits to indicate sub-product, like 01 for student's card, 02 for co-branded card with local department store and so on. In this case card prefix, which is basically product identifier, becomes 8 digits long. When I coded this part, I decided that 9 digits "ought to be enough for everyone". I was running ok for 2 years until one day bank make a new card products with 10-digit-long prefix (have no idea why they needed it). Not too hard to imagine what has happened - router segfaulted, the whole system halted because it cannot function without transaction router, all ATMs of that bank (one of biggest in the country) became non-operational for few hours, until problem was found and fixed.</p> <p>I cannot post the code here firstly because I don't have it and secondly it is copyrighted by the company, but it is not hard to imagine the <code>strcpy()</code> without checking size of target buffer.</p> <p>Just like <code>man strcpy</code> says:</p> <blockquote> <p>If the destination string of a strcpy() is not large enough (that is, if the programmer was stupid or lazy, and failed to check the size before copying) then anything might happen. Overflowing fixed length strings is a favorite cracker technique.</p> </blockquote> <p>I was very embarrassed. It was a good time to commit <a href="http://en.wikipedia.org/wiki/Sepukku" rel="nofollow noreferrer">seppuku</a> :)</p> <p>But I learned the lesson well and do not forget (usually :) ) to check size of target buffer. I wouldn't recommend you to learn it the hard way - just develop a habit to check target buffer before <code>strcpy()</code> and <code>strcat()</code>.</p> <p>Edit: good suggestion from Healthcarel - use <code>strncpy()</code> rather than <code>strcpy()</code>. It doesn't add trailing 0 but I usually use following macro to get around it:</p> <p><code>#define STRNCPY(A,B,C) do {strncpy(A,B,C); A[C] = 0; } while (0)</code></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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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