this post was submitted on 31 Aug 2025
316 points (96.5% liked)

Programmer Humor

26095 readers
1455 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
 

Finally I have a valid reason to learn about memory management. It was also hella weird when encountering it.

you are viewing a single comment's thread
view the rest of the comments
[–] cows_are_underrated@feddit.org 9 points 1 day ago* (last edited 1 day ago) (3 children)

This is the code I used:

#include <stdio.h>
#include <string.h>

#define MAX_ACCOUNTS 255

typedef struct
{
    unsigned int id;
    char account_creation_date [10];
    char first_name [255];
    char last_name [255];
    char country_code [2];
    unsigned int iban;
    char password [255];
    double balance;
} account;

account accounts_db[MAX_ACCOUNTS];
unsigned int accounts_created = 0;

account get_account_id (unsigned int id)
{
    int i = 0;
    while(i < MAX_ACCOUNTS)
    {
        if(accounts_db[i].id == id)
        {
            return accounts_db[i];
        }
        i++;
    }
    account account;
    account.id = -1;
    return account;
}

void create_account(char first_name [255], char last_name [255], char password [255], char country_code [2])
{
    account new_account;
    new_account.id = accounts_created;
    strcpy(new_account.first_name, first_name);
    strcpy(new_account.last_name, last_name);
    strcpy(new_account.password, password);
    strcpy(new_account.country_code, country_code);
    strcpy(new_account.account_creation_date, "");
    new_account.balance = 0.0;
    new_account.iban = 0;
    accounts_db[accounts_created] = new_account;
    accounts_created++;
}

int main()
{
    char first_name [255]  = "Max";
    char last_name [255] = "Mustermann";
    char country_code [2] = "DE";
    char password [255]= "password";
    create_account(first_name, last_name, password,country_code);
    account account = get_account_id(0);
    printf("Name: %s %s \n", account.first_name, account.last_name);
    printf("Account creation date: %s\n", account.account_creation_date);
    printf("IBAN: %s %d", account.country_code, account.iban);
}```

When you run it you can see, that behind the country code of the IBAN you get the first two letters of the surename
[–] LedgeDrop@lemmy.zip 16 points 1 day ago (2 children)

Without getting too critical of your code (congrats BTW), never use strcpy instead use strlcpy.

strcpy will happily allow you to create buffer overflows (a common challenge with C) which will cause your application to crash.

You'll find more details here.

Good luck!

Thanks, I did not knew this. I always appreciate constructive criticism. I am quite new to C so theres a shit ton of stuff I have never done or dont even know about.

[–] ozymandias117@lemmy.world 6 points 1 day ago

And understand when you can use them...

I've seen too much code following this advice blindly that just does something like

strncpy(dst, src, strlen(src))

[–] cows_are_underrated@feddit.org 8 points 1 day ago (2 children)

I found the mistake. Since the country code char array only has a size of 2 it overwrites the \0 char causing the memory to leak.

[–] silasmariner@programming.dev 16 points 1 day ago

Usually what's meant by a memory leak is memory that's allocated but never freed. Writing outside of array allocation would usually be considered an overflow. Which sounds kinda similar but is not the same.

[–] henfredemars@infosec.pub 1 points 1 day ago

Bingo. I thought something probably happened to your terminator.

[–] baduhai@sopuli.xyz 1 points 1 day ago

This could make for a fun reverse engineering CTF challenge.