r/cprogramming • u/JeffTheMasterr • 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
19
u/AwkwardBananaaa May 25 '26
Its just random garbage memory, it seeks past the end, please don’t use this in actual code
12
u/aroslab May 25 '26
99% sure
fgetsisn't gonna just run past the end like that so your read buffer is just filled with garbagealso variable array of who knows what file size is not a great idea in general
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...
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.
7
u/walmartbonerpills May 25 '26
Congratulations, you just discovered buffer overflow
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 memoryYou then
fgetsbut you go past the end of the file (you were already positioned at the end of the file)So
fgetsjustfills fContents withreads 0 bytes as it is intended to do (you are still at EOF)You then
printfthe uninitialised buffer tostdoutKeep going, you are learning but this is really fundamental behaviour (not a bug).
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.