this post was submitted on 25 May 2026
768 points (99.6% liked)

Programmer Humor

31554 readers
1808 users here now

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

founded 2 years ago
MODERATORS
 
you are viewing a single comment's thread
view the rest of the comments
[–] Kenny2999@lemmy.world 164 points 7 hours ago (1 children)
[–] akunohana@piefed.blahaj.zone 13 points 6 hours ago (4 children)

How would you do this in C? I'm a beginner. Does it entail checking/disallowing certain characters and data types? What? πŸ˜ƒ

[–] copacetic@discuss.tchncs.de 2 points 31 minutes ago* (last edited 30 minutes ago)

If you use the SQLite C API like this

    char query[256];
    snprintf(query, sizeof(query),
             "SELECT * FROM users WHERE username = '%s'", username);
    int rc = sqlite3_exec(db, query, NULL, NULL, &err_msg);

and someone enters Robert'; DROP Table Students;-- as username, it deletes the table Students.

    const char *sql = "SELECT * FROM users WHERE username = ?";
    int rc = sqlite3_prepare_v2(db, sql, -1, &stmt, NULL);
    if (rc != SQLITE_OK) {
        fprintf(stderr, "Failed to prepare statement\n");
        return;
    }
    sqlite3_bind_text(stmt, 1, username, -1, SQLITE_STATIC);

Using this "prepared statement" and "bind", your code is secured against such SQL injection attacks.

[–] LovableSidekick@lemmy.world 4 points 1 hour ago* (last edited 1 hour ago)

You wouldn't - what they're describing is called "SQL injection" - a way to fool poorly written web server code (regardless of what language it's writen in) into executing SQL code. The poorly written server code takes what's entered in a form field on a web page and pastes it into a skeleton of a SQL statement - in this case the text in the input field is SQL that ends the intended statement, followed by a new statement that deletes a table. For this to even work, the SQL skeleton on the server would have to be structured in just the right way so the modified version with the pasted-in text still makes sense. For this reason, hackers attempting SQL injection usually have to do a lot of trial and error to get something to happen. The only way it can work at all is if the server software handling the web page sends SQL commands to a database server as text, as if they're being typed in, and the server executes them. You can't inject C in this way because unlike SQL, C code isn't just executed, C programs have to be precompiled.

[–] jaybone@lemmy.zip 12 points 4 hours ago (1 children)

Many languages like C, Java, Python, etc allow you to construct SQL queries or SQL statements, where SQL is its own language used to communicate with a database, like Oracle or MySql, or Postgres or MSSQL. One way to do this is to construct a string in your language using whatever string functions, concatenation etc available in your language. The problem occurs because usually you want some kind of user input as one of the parameters in your sql query, in order to fetch the correct records the user is asking for. Like say a record ID or name. If you do not properly sanitize that ID or name which originally comes from some type of user input, then a malicious user could carefully craft an ID or name which includes their own SQL and other special characters, which will interfere with the query you intended to construct, and instead do something malicious. Like delete records or obtain records the user is not supposed to have access to.

There are many ways to guard against this, and you should learn about this when you start working with SQL and databases. It’s called a SQL injection.

There is another type of code injection which can occur if you are making exec() calls (or whatever your language uses) to run shell commands. Similar caution should be taken there.

[–] thesystemisdown@lemmy.world 5 points 3 hours ago (1 children)

I know what I'm dealing with when I see a query that isn't using a prepared statement.

[–] jaybone@lemmy.zip 3 points 2 hours ago

I mean a prepared statement is still created with a string.

But you definitely want to be using bind parameters with your prepared statements. Not only for security but also potentially performance improvements.

[–] vrek@programming.dev 21 points 5 hours ago (1 children)

How do you sanitize your inputs or how do you exploit inputs which are not sanitized.

[–] akunohana@piefed.blahaj.zone 18 points 5 hours ago (1 children)

Santize inputs.

I'll get back to you on exploits when I can write something that throws zero compilation errors. 😈

[–] vrek@programming.dev 15 points 5 hours ago (1 children)

Couple big things are 1. Only accept reasonable characters, on a white list instead of rejecting bad characters based on a black list. This will mean you are less likely to forget to block /0 for example. 2. Understand how strings work and ensure both reading and writing to that string doesn't extend beyond the end of memory allocated for the string. For example do you understand what the /0 would do to a string your program accepts?

[–] akunohana@piefed.blahaj.zone 6 points 5 hours ago (1 children)

Sic! Thanks! I'll work on this this weekend! 😊

[–] vrek@programming.dev 8 points 4 hours ago

Keep in mind, the lowercase and uppercase letters are in continuous blocks on the ASCII table so you can can use that to verify if a char is a letter without doing an incredible long chain of if else statements.