Code injection occurs when invalid data is injected as code instead of data and executed as part of a program. Common code injection techniques include buffer overflows, SQL injection, and cross-site scripting (XSS). Buffer overflows occur when input length exceeds the buffer size, overwriting the stack and potentially changing the return address. SQL injection happens when unsanitized user input is inserted into SQL queries, allowing manipulation of the database. XSS injects client-side script code by inputting it into a web application. To prevent code injection, developers must never trust unsanitized user input.