21 ms·
Yesterday I encountered a similar program on a HN comment chain as shown in this link. I am genuinely confused as to why this program is bad. I am a student and
by the_lurker 8y ago
Yesterday I encountered a similar program on a HN comment chain as shown in this link.
I am genuinely confused as to why this program is bad. I am a student and I do not know the best practices regarding pointers, but it is how I would write a program to combine two strings.
Can someone please elaborate why it is bad?
Are their any good resources to fill gaps in my knowledge?
Thanks in advance.
Edit: Thank you guys for pointing out so many problems. It seems that I have a lot to learn. :)
- saagarjha 8y agoThe C syntax is a bit too dated for me, but I think it returns a pointer to a stack buffer (which is capped at 100 bytes, so it could overflow) and goes through s twice? Plus it's not concise at all.
- TomVDB 8y agoIt returns the result r, which is a temporary, stack allocated array that becomes undefined by the time your return from the function. That’s the worst violation. Furthermore, r is a fixed 100 bytes. There is no overflow checking whatsoever.
- LukeShu 8y agoBesides stylistic (dang, that code is hard to read), efficiency (it scans through s twice), and safety (it will do bad things if the combined length is > 100) concerns, it's also just plain wrong: It returns a pointer to memory on the stack; but the stack will shrink when the function returns, and that piece of memory will get re-used. Which is consistent with the article's later claim that "I don’t think he understands the call stack."
- saulrh 8y agoNot a C programmer, so I'm sure I'm missing a ton of more subtle issues, but some of the immediate questions to ask: * What happens if s or t are longer than 100 characters? * What happens if s and t are both longer than 50 characters? * What happens if no element of s == '\0'? How about t? * r is allocated on the stack. What happens to the memory pointed at by r when you call another function after calling combine? Say you wanted to combine three strings; could you call combine twice to build up the result?
- grenoire 8y agoBack in the day it was pretty much granted you ended your strings with a \0.
- daemin 8y agoFor the first two points, you end up overwriting the stack and most likely crash. This is how security holes were/are made. For the third point, you have two main ways of storing strings in general. The first was called pascal style, where the length is stored first in either a byte or two and then the string data with no (or an optional) terminating null character (the '\0'). The second is referred to as c style, and is done by storing the string in a memory and denoting the end of the buffer by a null character. The c-style enables certain nicer looking c code with loops and such, but it is more dangerous and potentially expensive if you recompute the length all the time. You can always mix and match the way you store the string, such as in C++ where the string class stores a length as well as a null terminated string in a buffer. For your forth point, yes, you end up corrupting the string leading to more crashes and/or it can be used as an entry point to screw around with your program's stack.
- badrabbit 8y agoThere are a few bugs in it. It returns something on the local functions stack (r) which gets torn down after return. It uses the insecure strcpy allowing the caller to stack overflow the destination. It increments t in the loop after dereferencing it which only changes the value pointed to by t. x and y are improperly initialized, it null terminates r without checking for x's value(again a buffer overflow) and there is no upper bound limit checking in the for loop(second for() parameter). I'm sure there are other bugs too but I haven't looked at it too closely. But,yeah... It's a massacare.
- saagarjha 8y ago> x and y are improperly initialized How so?
- badrabbit 8y agoI said that because I forgot the strlen was there. I was wrong.
- Figs 8y ago> It increments t in the loop after dereferencing it which only changes the value pointed to by t. This is wrong. Postfix increment has a higher precedence than the dereference operator.
- badrabbit 8y agoAre you sure? I'll need to test this. Always thought postfix increment and decrement applied after everything else was evaluated,and that's why prefix versions exist.
- Figs 8y agoYes. Here is a table that lists operator precedence in C: https://en.cppreference.com/w/c/language/operator_precedence https://en.cppreference.com/w/c/language/operator_precedence Note that postfix increment is at the highest precedence and the dereference operator is at the second highest precedence. The postfix operators behave in a bit of an interesting way; the expression "t++" evaluates to just "t" but increments t as a side effect. Consequently, it appears to update t after the expression it is part of, even though it is one of the first things evaluated. If you actually test the function, you will find that it works provided that you make r static or global instead of a local stack variable -- and, of course, carefully mind the restriction on the length of the input strings to avoid overflowing the buffer.
- oddity 8y agoFrom a purely safety-minded perspective, this function has a hidden bound of r[] past which the function becomes unsafe, but it does not check. Not only does it not check, the design of the function means that there is no possible way for it to check safely. s and t are both pointers to characters? How long are the strings they might represent supposed to be? Who knows? This code is incredibly reckless and it's dangerous to introduce students to such carelessly designed examples when first impressions will define how they program for a few years or so. Ok, so it's reasonable to think that this is supposed to be a teaching example and that considering these concepts might be a bit too early in the process. This leads into the second problem and one that is more subjective: this code is incredibly dense and relies on enough quirks of C that it's almost never going to be clear to a beginner reader what they are supposed to take away from it. It's maybe useful as a quiz question on C syntax and semantics, but there are enough barriers to understanding what the code is supposed to do that the amount of explaining the text would need to do to describe what the code is doing is most likely prohibitively long. Instructive examples should be unambiguous in what they are trying to show, otherwise students will be confused and potentially conflate issues in a way that is difficult to untangle later. Edit: Ha! I spent so long looking at the first half of the function I totally missed that it was returning r! So, not only does this code have minor issues here and there from its careless implementation, it has a fundamental flaw that, if it were to work, would do so only by accident. I can only imagine that a student might walk away from this example thinking that C functions can return arrays and possibly misunderstand scoping in C.
- saagarjha 8y agoI'm sorry, but I don't think you've quite gotten the reasons why it was lambasted: > Not only does it not check, the design of the function means that there is no possible way for it to check safely. s and t are both pointers to characters? How long are the strings they might represent supposed to be? strlen > This leads into the second problem and one that is more subjective: this code is incredibly dense and relies on enough quirks of C that it's almost never going to be clear to a beginner reader what they are supposed to take away from it. Most "nice" C functions are much terser than this.
- 8y ago
- deleted 8y ago[deleted]
- deleted 8y ago[deleted]
- leodeid 8y agoI haven't touched C in years, but here's my descending "wtf" list: 1. Returns pointer to stack-allocated data, which immediately becomes invalid. Instead, it should be using some sort of allocation (e.g. 'malloc'), or taking in a destination pointer. 2. 'r' is arbitrarily set with length 100. Smaller strings don't need all that space, and larger strings definitely will overrun. 3. The function signature is really awkward. Without any of the surrounding textbook content, I'm not sure what behavior is supposed to happen. At first, I expected something like 'strcat', which takes two char* and appends the second one to the first one. But that isn't happening here and instead it seems to require dynamic allocation. (Hiding allocations inside a function is generally kind of weird. Usually the caller should be responsible for passing in a handle to the destination.) 4. There's no sensical limit on the loop iteration. If the input 't' doesn't have a null terminator, this is going to throw a ton of garbage into the stack space (because 'r' is stack-allocated to a fixed size). And also maybe run for a really long time. 5. 'strcpy' should usually be replaced by 'strncpy', which performs the same function but also requires you to provide a limit ("copy this string, but at most 'n' bytes"). That prevents a class of exploitable errors known as "buffer overruns". I don't know when the 'n' string functions were added to C or became popular, though. This is a teaching exercise, so the fact that this is implemented as a separate function instead of calling 'strcat' from <string.h> doesn't seem like a big problem.
- saagarjha 8y ago> If the input 't' doesn't have a null terminator Then it's not a string.
- konstmonst 8y agoThere are multiple problems with that code: 1) return(r) He is returning pointer to temporary! When you declare and initialize variables on stack after that function, they will overwrite memory pointed by r. 2) He is assuming size of string pointed by s(including 0) is less than 100 and also the combined sizes of s and t are less than 100. Stack Overflow! 3) Not an errors, but would not pass my code review: Inconsistent Variable declaration, using signed integer for loop where you don't need signed. Also I would have used strcpy 2 times, or loop 2 times, not two different ways. Beside that, you should not use strcpy, but strncpy to avoid stack overflows.
- saagarjha 8y ago> Inconsistent Variable declaration How so? > I would have used strcpy 2 times You can't do this, because strcpy doesn't give you the length of the string you copied, which is necessary to put the trailing null byte.
- int0x80 8y agoIt is wrong in many ways. It copies s and then t to a fixed size buffer, without any checks. That will write to invalid memory (probably smashing the stack) if len(s), len(t) or len(s) + len(t) > 100. It returns a stack allocated buffer (r) pointer to the caller. The array will be invalid when the function returns, as the automatic variables only live in the function scope (during the call), they are deallocated when the function returns. To do this right you have various strategies. 1. Allocate a buffer of len(s) + len(t) + 1 with malloc, copy the strings and return it. Have the caller free it when it's done. It can be inapropiate because of the dynamic allocation. 2. Have the caller pass a destination buffer and its size. If you know the char *'s are zero terminated, check if you have space for them in the dest buffer. If not, truncate or error out. Most of the times, this is the prefered solution. 3. Use a static local buffer, and return it to the caller. You may need to truncate the copy too. Not recomended. This is not a good solution as the function also will not be reentrant (unsafe with multiple threads). You can use libc functions like strncpy (C89+), snprintf (C99+) etc to make a "size-checked" copy with various automatic truncation semantics. You can refer to their man pages for details. Edit: to the downvoters, please point out what is wrong in the comment.
- jmts 8y agoNot sure why you're being downvoted. That's all pretty standard. Method 1 is a contract you often make as part of defining the interface. For an example, such things are out of scope so this is a perfectly reasonable point. Method 2 is common. Most of the strn*() and snprintf() etc, do this. This is the preferred method for some of my colleagues. Method 3 is used in the standard library, though as mentioned is usually avoided.
- saagarjha 8y agoI'd suggest not insulting the people reading your comment if you want some points back…
- int0x80 8y agoYou are right. Fixed.
- 8y ago
- jmts 8y agoMost of these points are covered by the other comments. As a C programmer professionally, I'll go into a little more depth, and offer an alternative implementation for comparison. The function in question: char *combine(s, t) char *s, *t; { int x, y; char r[100]; strcpy(r, s); y = strlen(r); for (x = y; *t != '\0'; ++x) r[x] = *t++; r[x] = '\0'; return(r); } 1. The array 'r' is allocated on the stack, and returned from the function. This is bad because 'r' goes out of scope as soon as the function is returned. This function returns a pointer to memory with essentially unknown contents. 2. The array 'r' is allocated at a fixed size. This is okay if you know ahead of time that you know this size, however for this function we don't know the lengths of s and t, so the odds that we are allocating the right amount of memory is slim. 3. As a result of points 1 and 2, the strcpy and loop may cause a buffer overflow. This is a class of bug where it is possible to overwrite memory that should be unavailable to us. In this case, if the combined lengths of s and t happen to be equal or greater than 100 characters, we will be overwriting memory that does not belong to r, corrupting it, and potentially crashing the program. This may additionally be as security risk, as buffer overflows can be exploited to execute malicious code. 4. There are no checks to see whether s and t are valid pointers. If they are NULL then the function would generate a segmentation fault. This is a check that is often ignored in cases where it is deemed to potentially hinder performance if the function is used frequently. saulrh also mentions the case where s or t are not NUL-terminated. This is often considered to be a pre-condition of the function in C, suggesting that providing the function strings that aren't NUL terminated is an issue for the user. For some comparison the following would be my first cut at the same function. Note I the comments are only for illustrative purposes. I'd omit them in actual code. char* combine(const char *s, const char *t) { size_t slen, tlen; char *str; // optional checks for validity (point 4) if (NULL == s || NULL == t) { return NULL; } // get lengths of s and t, to calculate allocation size // also save them to use with memcpy later slen = strlen(s); tlen = strlen(t); // allocate to heap (point 1) // allocate correct size (point 2) str = malloc(slen + tlen + 1); if (NULL == str) { return NULL; } // use memcpy since we already know the size memcpy(str, s, slen); memcpy(str + slen, t, tlen); str[slen + tlen] = '\0'; return str; }
- flukus 8y ago> Can someone please elaborate why it is bad? Are their any good resources to fill gaps in my knowledge? If you're actually learning C and writing programs in it then the two best things to do would be turn compiler warnings up to 11 and run valgrind often. So you want -Wall and -Wpedantic when you compile and if possible run with valgrind as part of your build script or run it with your tests, just run it often (the longer between runs the harder it is to track back to which change you made).
- saagarjha 8y agoOr -fsanitize=address, if your OS does not support Valgrind.
- aaronmdjones 8y agoNote that you forgot '-Wextra', but even `gcc -Wall -Wextra -Wpedantic` doesn't enable all diagnostics (there are some 40 additional arguments to provide, depending on GCC version); so what you really want is a recent version of Clang and '-Weverything'.
- archgoon 8y agoPutting my CR hat on: 1 char *combine(s, t) 2 char *s, *t; 3 { 4 5 int x, y; 6 char r[100]; 7 8 strcpy(r, s); 9 y = strlen(r); 10 for (x = y; *t != '\0'; ++x) 11 r[x] = *t++; 12 13 r[x] = '\0'; 14 15 return(r); 16 17 } There are several critical memory errors here. 1) The function is returning the address of a local variable. This alone makes this function rubbish. 2) The pointers s and t are unknown length, we have no guarantees that concatenating them will fit in a 100 character array. Also, he probably should be using malloc to dynamically allocate the space. 3) Use of the function strcpy rather than strncpy. He should have measured the length of s first, and then use strncpy if s was longer than 99 characters (don't forget the null terminator!). Then, rather than using a for loop, call strncpy again to copy the rest into a safe buffer size (the for loop is rather silly). The reason for this is that strcpy will cheerfully start copying past the array 'boundaries', and in this case, since he's copying into a local variable on the stack, is setting himself up for a Remote Code Execution attack if this ever gets untrusted input. So those are the critical errors. These tie directly into why Geoff argues that the author doesn't understand the stack. So let's, for educational purposes, go into this. We're going to go a bit into the weeds here. Sorry about that. This would be easier with a whiteboard. :) When you fire up a program, the programs machine instructions get copied into memory, let's pretend at the memory location 0x1000. Far away from that code, at the highest memory values (more complicated on modern virtual systems, but hey, let's go back in time here :) ), the computer keeps track of a location called the stack pointer. I'm going to put forward 3 diagrams now. Please forgive any off by one errors. (Diagram a) Registers A 0 B 0 C 0 SP 0xffff PC 0x1000 Address Mnemonic DATA PC0x1000 MOV 1, A 0x00 0x01 0x01 0x01 0x1004 MOV A, C 0x00 0x01 0x03 0x01 0x1008 PUSH 3 0x01 0x00 0x00 0x03 ... ... ... ... ... ... 0xfffc XXXXXXXX 0x00 0x00 0x00 0x00 <-- Stack Pointer is here The program starts at 0x1000, then after executing the first two move (MOV) instructions, the state of the world becomes as follows (Diagram b) Registers A 1 B 0 C 1 SP 0xfffe PC 0x1000 Address Mnemonic DATA 0x1000 MOV 1, A 0x00 0x01 0x01 0x01 0x1004 MOV A, C 0x00 0x01 0x03 0x01 PC0x1008 PUSH 3 0x01 0x00 0x00 0x03 ... ... ... ... ... ... v------\ 0xfffc XXXXXXXX 0x00 0x00 0x00 0x03 ^-- Stack Pointer is here When you have code like void function() { int a = 5; int b = 2; return a; } void main() { return function(); } It'll get turned into something like (I've set a 'break point' at 0x2010) (Diagram c) Registers A 5 B 0 C 0 SP 0xfff8 PC 0x2010 Address Mnemonic DATA # Main starts here # (Note, in C, there is actually code that gets executed before this) PC0x1000 PUSH 0x1008 # We want to remember where to return to, so we push it to the stack. 0x1004 JMP 0x2000 0x1008 EXIT A # In this implementation of C, the A register will propagate return values ... ... # Function 'function' is here 0x2000 PUSH 5 # Local variables go on the stack. 0x2004 PUSH 2 0x2008 MOV [SP+2], A # Locally, we refer to local variables by # offsets to the stack pointer, so if this function # were to call itself, the stack would keep growing down # but these values would be good. 0x200c MOV SP+2, SP # Reset the stack before returning PC0x2010 JMP #SP # Made up notation. Look at the value of the stack pointer, pop it, and jump to it. # in x86, this is kinda what RET does. ... ... ... ... ... ... ... ... 0xfff8 XXXXXXXX 0x00 0x00 0x00 0x00SP 0xfffc XXXXXXXX 0x02 0x05 0x10 0x08 Okay! So with the above diagrams in mind, let's recap what goes on the stack. Local variables and return addresses. Each time a function gets called it moves the stack pointer down[1] (to lower memory addresses) to make room for local variables. So after you return from that function, and then call another function (or heck, the same one) that pointer you have that was supposed to be the concatenated string is now going to have it's values overwritten. Furthermore, if the input strings are longer than expected, than they can overwrite values on the stack itself, including the return addres, causing your program to jump to some (if you're lucky) random location in memory. Honestly, some of the best ways to get intuition for how the stack works, and the things that can go wrong, are CTFS at overthewire.org. Also, https://microcorruption.com/ https://microcorruption.com/ http://overthewire.org/wargames/bandit/ http://overthewire.org/wargames/bandit/ http://overthewire.org/wargames/leviathan/ http://overthewire.org/wargames/leviathan/ [1] Sorry, 'down' means lower memory addresses, even though the displays of memory layouts always have lower memory addresses "up". :(
- userbinator 8y agoI know there are already plenty of comments about the problems but might as well give mine too... In one sentence: this guy managed to reinvent a variant of strcat and get it completely wrong. Here's a somewhat better version: char *combine(char *s, char *t) { size_t m = strlen(s), n = strlen(t); char *ret = malloc(m + n + 1); if(ret) { memcpy(ret, s, m); strcpy(ret + m, t); } return ret; }