Here is me, at the first year of college, I remember failing to use strcat because i did not understard how pointers work at the time. Then i decided to write those strings to a file and read it back lol
ignoring your pun (well done, btw), I sincerely always believed that coding is better taught via a mentoring/apprenticeship system than it is in a classroom. There's a certain amount of theory necessary of course, and that increases as you get to more advanced concepts. Being said, as an industry we know that there are really only two things necessary to being a competent mid-level coder: reading good code, and writing code to be reviewed by good coders. Why we don't enforce this from the beginning is beyond me, and it's likely to do with HR's flawed insistence that a degree is a quick and easy way to tell if someone is competent.
Thanks for the compliment. Besides wholeheartedly agreeing with you here, I also generally try to get things like that across via humor. Hence the pun :).
Now that I do understand pointers I don’t understand how I ever didn’t understand them lol. Like, it’s a variable that points to a place in memory. Sometimes it’s the start of an array. What didn’t I get?
Well, I think what complicates the things for new comers is maybe they usually learn many other obscure related concepts at the same time they learn pointers like arrays, memory allocation, buffer overflow, pointer arithmetic, file operations, etc… maybe if we had just learned pointer variables and pointer dereferencing at the beginning when we learned scanf we would have a better intuition. Maybe
In my case, understanding what pointers do is not the problem - that is easy enough. What is hard for me is to actually use them correctly, especially when using malloc and friends for dynamic memory allocation.
Because you were taught that variables referenced some piece of data not some piece of memory. So when someone told you what a pointer was it was confusing as hell because now variables where some place in memory and pointers were some place in memory that stored the address of another place in memory.
Things like pointers of pointers, pointer arithmetic, pointing to functions and many other things. The concept is easy to understand but not that easy.
sometimes it takes some experience to be able to step across that second layer of abstraction from referencing what's in the bucket to referencing the bucket itself.
So the actual IO operation of the concatenation seems safe (I think?), but what is being concatenated isn't. You can get it in a debugger and break before reopening, modify the file, then continue the program.
Another way but kinda more difficult would be to get into a race condition with the file closing and reopening.
Just thinking about it from a security side of things.
You can snprintf directly into emailBody if you know emailBody does not overlap surname or partOfName at all, saving a buffer and an additional copy.
But, yeah, I agree, strncat is just a landmine. It takes a size, but the size is not the size of the destination buffer, it's the size of the input -- and it's a size_t which is an unsigned type, so if you do subtraction math to it, you have to worry about underflowing 0.
Even strncpy is a landmine, too. It takes a size, but does not guarantee the destination is null-terminated. So, while something like this seems fine:
If surname is longer than 50, emailBody won't be null-terminated, which will cause strlen to fail. "Oh, that's fine, just use strnlen instead," I hear you say:
Well, you still have the unterminated problem of passing emailBody into strncat, but you also have an underflow issue, too -- If emailBody is longer than 50, strnlen will return 50, 50 - 50 - 1 = UINT_MAX.
209
u/[deleted] Nov 26 '21
Here is me, at the first year of college, I remember failing to use strcat because i did not understard how pointers work at the time. Then i decided to write those strings to a file and read it back lol
49
u/_pelya Nov 26 '21
This code is easy to understand, at least.
28
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Nov 26 '21
Image Transcription: Code
I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!
87
u/Botahamec evil Nov 26 '21
Ngl. This is probably the only safe way of concatenating strings in C
8
u/microwavedHamster Nov 26 '21
beautiful
4
u/fliguana Nov 27 '21
Not MT safe. Should have used streams. /s
2
u/Scud000 Nov 27 '21
I almost thought it was Go-lang code.
1
u/bubbleztoo Nov 27 '21
Why not open to read write!?
1
u/notddh Nov 27 '21
wow this is so bad. they didn't even use the tmpfile function to create the temporary file...