Go Down

Topic: after runShellCommand() no more console output (Read 580 times) previous topic - next topic


Nov 05, 2018, 09:46 am Last Edit: Nov 05, 2018, 11:37 am by GiovanniG11
Hi, I.m running a Sketch using RasPiArduino, the date command is executed, but after it I can't more use Console.print, the code is runned but can't see anything more in my SSH session.
Kind help, thank you

Process Set;
char command[34];
sprintf(command, "date --set '%02d-%02d-%02d %02d:%02d:%02d' \0",
2016, 12, 19, 05, 02, 01);
Console.print(command); //I can see it
Set.runShellCommand(command); //executed
Console.print(command); //I can't see it
lcd.print(command); //I can see it


Most of it looks good to me, except that you have a buffer overrun in your sprintf() statement - you are trying to stuff 35 characters into a 34 character buffer. There's no telling what problems that may cause, and there isn't much point in trying to figure out why other things aren't working until that is fixed.

I didn't see it at first, because if you count the characters in the format statement, it looks like you have extra room, but you don't. Two gotch-as that I see:
  • the first conversion, %02d, looks like it would add two characters, but you are passing in a value of 2016, so it will actually consume four characters of your buffer (the width specifier only indicates a minimum field width, not a maximum.)
  • You will have two null terminators on your formatted output - the \0 you explicitly added at the end, and the null that automatically gets added by the print operation.

In general, while it makes formatting complex strings easier, sprintf() is a dangerous function. Consider using snprintf() instead, or be extremely careful about your formats, and leave yourself lots of extra space.


Thank you for your kind answer, I'll correct it to 35 but I'll not see diferences I guess, because the command works and the next code is executed, just I lose the output of the console messages forever on my ssh, I can't see any more the console print messages, how to overcome it?
Thank you!


I can't see any more the console print messages, how to overcome it?
Don't worry about that until you fix the buffer overrun.

There is no telling what kind of problems a buffer overrun can cause. A buffer overrun is particularly insidious because the issues it causes are not necessarily immediate, and they don't necessarily make sense. In this case, you are running over the end of the buffer, and zeroing out a single byte of memory that is just past the end of the buffer. What is that byte being used for? It could be a return address on the stack, in which case you're not going to have a problem until you return from that function and the code jumps to the wrong address, either crashing the application or causing odd behavior. Or maybe it's one of your other variables that are inexplicably changing values.

Or perhaps in this case, that byte that occurs after your buffer is a pointer that is being used by the Process object (since it's declared immediately adjacent to your buffer, this is quite likely.) By running over the buffer, you've changed this pointer, and now it points to some different section of memory. That isn't an immediate issue until you run the Process, at which point it may access that pointer and corrupt some other memory - perhaps some variable inside the Console object so now it suddenly doesn't work.

Yes, that sounds like a contrived scenario, and what's going on is probably quite different. My point isn't to try and figure out why a buffer overrun could cause the Console to work at first, and then fail later. My point is that you really have no idea what kind of problems a buffer overrun might cause. I've been programming for 41 years now (professionally for 34 years) and I've seen many different buffer overruns, all with different symptoms. About the only thing that they had in common was that the problems they caused were completely unpredictable, and they usually didn't happen right away - there would be the buffer overrun, but strange things wouldn't happen until something else unrelated happened first.

I have no problem believing that a buffer overrun could be causing your problem. It may not be the case, but many hard lessons in my experience have taught me not to fret about strange things until the buffer overrun is fixed. A vast majority of the time, the strangeness goes away once the buffer overrun is resolved.

Fix the overrun first. If your problem still persists, then you can worry about the Console class suddenly not working.

Go Up