You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
First of all, thank you very much for the book @JLospinoso. Amazing work!
Bug information:
Although Listing 4-27 produces the correct result, there is a subtle bug. The bug is at line 49 "SimpleString a{ 20 }; " or at line 45 "x.append_line("This change is lost.")" depending on your interpretation.
Explanation:
Currently when calling foo in main, we cause SimpleString::append_line to run. In there we check to see if the length of the string to append to, plus the string to append, plus 2 is greater than max_size. Currently this always evaluates to true which means we return false on line 30 and never run std::strncpy(buffer + length, x, max_size - length) on line 31. What this mean as a consequence is that when main finishes it prints the correct result because we never got to append anything in "a" initialized on line 49 not because of the function call "foo(a)" on line 50 which causes the copy constructor on line 18 to run and to create a temporary and completely independent SimpleString object (Figure 4-5). To reveal the bug, comment out the copy constructor
to go back to the situation illustrated in Figure 4-4. This should now cause the copy constructor on line 18 to create a temporary SimpleString object with a "buffer" variable that points to the same "buffer" variable contained in "a" (Figure 4-4).
However the variable "buffer" (which is the same for both a and the temporary object in this case ) never gets updated with the string "This change is lost." passed on line 45. That's because the string "This change is lost." is 20 characters long. "a" initialized on line 49 therefore has max_size equal to 20 which is used in the creation of the temporary object. Consequently the expression if(x_len + length + 2 > max_size) on line 29 always return true which cause line 31 to never run.
Two Easy fixes
1.) declare "a" with a default size of 22 e.g SimpleString a{ 22 } or
2.)change the string passed on line 45 to a string that is at most 18 characters long e.g x.append_line("This change is los"); this is just for illustration purposes.
Steps to reproduce the bug:
1.) declare "a" with a default size of 20 e.g SimpleString a{ 20 };
2.) Comment out the copy constructor
3.) build Listing 4-27
4.) Run a debugger on the executable produced by the build
NOTE: for convenience the file boggy_code_debugging_output.txt containing GDB debugging output has been provided; see that line 31 is never called and this is the main reason why we get the correct result after main finishes.
Steps to verify that the bug is fixed
1.) Apply one of the easy bug fixes above
2.) Uncomment the copy constructor
3.) build Listing 4-27
2.) Run a debugger on the executable produced by build
Note: for convenience the file corrected_code_debugging_output.txt containing GDB debugging output has been provided; see that line 31 is now called. And after the copy constructor creates and appends to the temporary and completely independent SimpleString object, a's variable “buffer” is unaffected.
First of all, thank you very much for the book @JLospinoso. Amazing work!
Bug information:
Although Listing 4-27 produces the correct result, there is a subtle bug. The bug is at line 49 "SimpleString a{ 20 }; " or at line 45 "x.append_line("This change is lost.")" depending on your interpretation.
Explanation:
Currently when calling foo in main, we cause SimpleString::append_line to run. In there we check to see if the length of the string to append to, plus the string to append, plus 2 is greater than max_size. Currently this always evaluates to true which means we return false on line 30 and never run std::strncpy(buffer + length, x, max_size - length) on line 31. What this mean as a consequence is that when main finishes it prints the correct result because we never got to append anything in "a" initialized on line 49 not because of the function call "foo(a)" on line 50 which causes the copy constructor on line 18 to run and to create a temporary and completely independent SimpleString object (Figure 4-5). To reveal the bug, comment out the copy constructor
to go back to the situation illustrated in Figure 4-4. This should now cause the copy constructor on line 18 to create a temporary SimpleString object with a "buffer" variable that points to the same "buffer" variable contained in "a" (Figure 4-4).
However the variable "buffer" (which is the same for both a and the temporary object in this case ) never gets updated with the string "This change is lost." passed on line 45. That's because the string "This change is lost." is 20 characters long. "a" initialized on line 49 therefore has max_size equal to 20 which is used in the creation of the temporary object. Consequently the expression if(x_len + length + 2 > max_size) on line 29 always return true which cause line 31 to never run.
Two Easy fixes
1.) declare "a" with a default size of 22 e.g SimpleString a{ 22 } or
2.)change the string passed on line 45 to a string that is at most 18 characters long e.g x.append_line("This change is los"); this is just for illustration purposes.
Steps to reproduce the bug:
1.) declare "a" with a default size of 20 e.g SimpleString a{ 20 };
2.) Comment out the copy constructor
3.) build Listing 4-27
4.) Run a debugger on the executable produced by the build
NOTE: for convenience the file boggy_code_debugging_output.txt containing GDB debugging output has been provided; see that line 31 is never called and this is the main reason why we get the correct result after main finishes.
boggy_code_debugging_output.txt
Steps to verify that the bug is fixed
1.) Apply one of the easy bug fixes above
2.) Uncomment the copy constructor
3.) build Listing 4-27
2.) Run a debugger on the executable produced by build
Note: for convenience the file corrected_code_debugging_output.txt containing GDB debugging output has been provided; see that line 31 is now called. And after the copy constructor creates and appends to the temporary and completely independent SimpleString object, a's variable “buffer” is unaffected.
corrected_code_debugging_output.txt
The text was updated successfully, but these errors were encountered: