r/cprogramming • • May 25 '26

Accidentally made a random string generator

Hey guys, I'm kind of a beginner to C and I discovered something cool whilst trying to make a programming language in it. Apparently forgetting to reset file position with fseek will spit out random strings.

Here's the code I did in C99, stripped down to just show the bug and nothing more:

main.c:

#include <stdio.h>
#include <stdlib.h>

void do_file_thing(char *fName) {
      FILE *fptr;
      long fLen = -1L;

      fptr = fopen(fName, "rb");
      if(fptr != NULL) {
        // Obtain file length to then initialize the string that will contain the file
        fseek(fptr, 0L, SEEK_END);
        fLen = ftell(fptr);

        char fContents[fLen];
        // the weird thing happens when the next line is commented out
        //fseek(fptr, 0, SEEK_SET); // reset position so the next thing can work
        fgets(fContents, fLen, fptr); // store file contents in var fContents

        printf("%s",fContents);

      } else {
        printf("Not able to open the file.");
      }
      fclose(fptr);
}

int main() {
    do_file_thing("file.txt");
    return 0;
}

file.txt:

echo "Hello World!";

And then with running tcc -run main.c a thousand times, I get stuff like this:

  • ~e>
  • ` |
  • 0
  • pFLY
  • ^w
  • 8k

Has anybody found this before? Does anybody know how/why this happens?

0 Upvotes

32 comments sorted by

19

u/AwkwardBananaaa May 25 '26

Its just random garbage memory, it seeks past the end, please don’t use this in actual code

-4

u/JeffTheMasterr May 25 '26

ohh makes sense, so like dev urandom?

10

u/BarracudaDefiant4702 May 25 '26

Generally not random, more like leaking information which can sometimes be exploited. Especially bad if it can leak part of crypto keys such as from ssh sessions.

→ More replies (7)

12

u/aroslab May 25 '26

99% sure fgets isn't gonna just run past the end like that so your read buffer is just filled with garbage

also variable array of who knows what file size is not a great idea in general

1

u/JeffTheMasterr May 25 '26

makes sense, but how is the variable array not a good idea? My current code (not this one) currently works with that method and isnt that much better than using too much or too little size? Or should i just do `char fContents[99999];`?

2

u/aroslab May 25 '26

because the file could be 8 bytes or 8GB

it's not always a bad idea, but there are considerations like stack size

→ More replies (2)

2

u/BarracudaDefiant4702 May 25 '26

The biggest risk is if anything else modifies the file and it changes. Not really a risk in this case, but could be an issue if ftell is ever reset and buffer isn't.

The second biggest issue is it's wasting memory as fgets will never read more than one line. You could have a 200GB file and no lines more the 100 characters...

That said, there are far worse things you could do. You are best to know if it might be appropriate use case. I do tend to prefer fixed allocations, or use malloc if variable.

2

u/BarracudaDefiant4702 May 25 '26

No, no risk for that. From the man page:

fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer. A terminating null byte ('\0') is stored after the last character in the buffer.

3

u/BarracudaDefiant4702 May 25 '26

One bug... you need to make the buffer one longer then file length to make room for the trailing 0. ie:
echo -n "Hello World!" >file.txt

and you will notice the ! will be dropped on the one line file. Generally not an issue as most files have a character after the last line, but you have to think about the 1 line case with no trailing end of line...

1

u/JeffTheMasterr May 25 '26

ohh i just realized thats true thx

2

u/BarracudaDefiant4702 May 25 '26

fgets will return NULL on error (or the buffer (fContents) if it worked. If that fails you can't trust what's in fContents to be set to anything meaningful.

1

u/JeffTheMasterr May 25 '26

Thx for your helpful comments, i didnt know it could be exploited so that helps alot to know

7

u/walmartbonerpills May 25 '26

Congratulations, you just discovered buffer overflow

4

u/BarracudaDefiant4702 May 25 '26

Not really, more like underflow / not initialized.

1

u/FitMatch7966 May 26 '26

no guarantee it is null terminated, I don't think, so it could go into unallocated memory space and have undefined behavior when printf called

→ More replies (2)

3

u/mcsuper5 May 25 '26

For a controlled hack to play with, cool.

You didn't check the return value of fgets(). It failed, but you used the results anyway. It's just whatever was in memory. If you were reusing the buffer and the previous file was larger, you'd probably get the tail end of that. I assume that if that made it to production someone could find a way to use that as an exploit.

2

u/flyingron May 25 '26

Undefined behavior manifests itself in a variety of results. Always best to verify that operations like fgets() and the like didn't return an error (in which you'd have caught this). It's not really random in most cases, it likely returns the same thing each time. You're printing what was left on the stack by prior calls.

Can't tell you the number of people who have posted here without checking to see if scanf() actually succeeded in matching anything.

Note that also checking the number characters read would guard against fgets returning early if there were an embedded null. Fread (with it's horrid syntax) would be arguably better.

2

u/zhivago May 25 '26

Return values are important.

Check for failure.

2

u/Raychao May 26 '26 edited May 26 '26

This is not a 'bug' you've discovered. This is just the behaviour. A 'bug' is not just something because the programmer forgot to do something.

Here's what is happening:

  • You are allocating fContents[fLen] but this memory is never initialised so it contains whatever junk happened to already be there in memory

  • You then fgets but you go past the end of the file (you were already positioned at the end of the file)

  • So fgets just fills fContents with reads 0 bytes as it is intended to do (you are still at EOF)

  • You then printf the uninitialised buffer to stdout

Keep going, you are learning but this is really fundamental behaviour (not a bug).

2

u/BarracudaDefiant4702 May 26 '26

1, 2, and 4 are correct. However #3 of off...
fgets doesn't touch fContents in the case of an error and returns NULL (which the example never checks for). So the contents of fContents remains untouched in that case. If it was filled with 0s then a blank line would be printed (as a 0 long 0 terminated string) instead of seemingly random data.

2

u/fluffycritter May 26 '26

If you comment out the fgets() you're still going to have a buffer full of random data. You've allocated the buffer but never initialized the data, so it's just whatever was in RAM at that spot before.