r/badcode • • Sep 07 '19

c++ Value not found? Let's just hang your processor

Post image
688 Upvotes

48 comments sorted by

236

u/[deleted] Sep 07 '19

This is a valid technique in embedded. Watchdog assisted reset.

5

u/Geend Sep 07 '19

Well this is two layers deep in a lib. The behavior is not documented anywhere. And maybe the mcu has to do other things as well? Also just resetting the whole damn thing as the only form of error recovery sound a bit to much as well

Edit: Totally forgot about the watchdog when I made the original post. So not as bad as I thought initially, but still...

11

u/deelowe Sep 07 '19

But still what? Embedded programming does stuff like this all the time. It's pretty standard practice.

-1

u/Metallico9 Sep 07 '19

The fact that it's standard practice does not mean it's good practice. Having an un-notified error that is not documented or notified in any way can be a nightmare when maintaining/debugging.

4

u/deelowe Sep 07 '19

How would you know if there's any debugging information without the relevant files?

60

u/hooahest Sep 07 '19

TIL , thanks

Wouldn't it be better to have a segmentation fault? Or at least make it into a function called Reset() or something

44

u/thenickdude Sep 07 '19

A lot of (small) embedded devices don't have memory protection, so there's no segmentation fault.

28

u/[deleted] Sep 07 '19

I mean the watchdog is the reset. It's probably easier to restart the process with a built in mechanism than write all that out yourself. A segfault will basically do the same thing since the process won't be responsive after it crashes.

0

u/[deleted] Sep 07 '19

[deleted]

4

u/oldrinb Sep 07 '19

some embedded systems have MMUs that support paging

54

u/EncapsulatedPickle Sep 07 '19

The best submissions to badcode are the upvoted ones where someone then tells us that it isn't actually bad code.

12

u/[deleted] Sep 07 '19

Was about to say this.

20

u/balenol Sep 07 '19

Care to explain why please?

0

u/[deleted] Sep 07 '19

[deleted]

1

u/anghenfil Sep 13 '19

But why don't stop the whole program? So you could just check if the process terminated and then start again.

0

u/[deleted] Sep 13 '19

[deleted]

1

u/anghenfil Sep 13 '19

So they just get rebooted from outside if they don't respond?

1

u/anghenfil Sep 13 '19

Or could you just trigger a restart via software?

0

u/[deleted] Sep 13 '19

[deleted]

1

u/Prom3th3an Sep 20 '19

Can't it just be a processor instruction? Just have it raise the same interrupt that the watchdog raises.

8

u/blipman17 shameless Sep 07 '19

Why not make a specific descriptive function to do this hanging instead of requiring the programmer to guess if someone wrote the loop wrong or actually wanted the program to crash?

20

u/mcergun Sep 07 '19

You don't have to guess when you are in the industry. it's somewhat of a common technique.

3

u/fb39ca4 depraved Sep 07 '19

Or when you have a debug interface you can pause and see where the error happened.

1

u/F54280 Sep 07 '19

I would feel much better without the -1 at the end of the for loop... maybe there is a sentinel value at the end of the array, but it doesn’t look very right...

1

u/tending Sep 07 '19

Still, shouldn't it be inside the implementation of an assert macro?

0

u/[deleted] Sep 07 '19

[deleted]

15

u/lIlIllIlll Sep 07 '19

I agree but with embedded every situation is a least slightly different than another. For this particular case it's important to remember that some embedded devices don't have memory protection, so hanging here could actually be pretty good.

9

u/equal_odds Sep 07 '19

Is it just me or does it feel weird to see C++ with VS Code syntax highlighting? Or is this not cpp

2

u/Geend Sep 07 '19

Yes this is vscode with the platformio plugin. Works quiet well out of the box. I'm just wishing for a bit more refactoring support

11

u/developedby Sep 07 '19

Looks normal to me

8

u/[deleted] Sep 07 '19

Write all my C++ in VS Code. It’s not that hard to set up some tasks to compile it for you, and I did it all manually, including creating folders and headers the way I like to organize my projects.

3

u/equal_odds Sep 07 '19

Ahh I miss having header files. Such a C quirk

7

u/SirNoodlehe Sep 07 '19

As far as I know, it's good practice to use header files for your classes in C++

1

u/[deleted] Sep 07 '19

Did I understand you wrong, or do you not use header files in C++?

4

u/equal_odds Sep 07 '19

Nah I just don’t use C++ these days haha

1

u/tcpukl Sep 07 '19

It's in c++ too. Hardly a quirk, it's been around for 50 years.

4

u/incomingstick Sep 07 '19

Im not a huge fan of the default colorscheme. Im a ricer so I just made my own vscode theme that matches the rest of my desktop/terminal colors

2

u/tcpukl Sep 07 '19

What else would you use? Vi?

3

u/Tynach Sep 07 '19

Dunno about them, but I use Kate.

I don't like the idea of an electron app text editor or IDE.

2

u/TheUnlocked Sep 07 '19

I get why you say that, but VSCode is actually really performant (for normal-sized files) and has a lot of really nice features. I have an instance or two running in the background at all times and ATM they're only eating up about 50MB of RAM each.

1

u/Tynach Sep 07 '19

And they almost always have shitty font rendering on Linux. Usually there's no subpixel anti-aliasing, and the fonts aren't rendered with gamma correction, so the clarity and stroke widths look inconsistent and janky.

Additionally, 50 MB is not the worst in the world I suppose, but Chrome (and Electron) tend to start many background processes that are separate from the main process. Be careful how you get those numbers - they're likely higher than you think.

1

u/tcpukl Sep 07 '19

I've never even heard of Kate. But we use visual studio in the games industry.

2

u/Tynach Sep 07 '19

Visual Studio Code is not the same as Visual Studio. Except for the name, the two are practically unrelated.

Kate is an advanced text editor that's part of the KDE project. There's a Windows version, but most of the time it's used on Linux.

17

u/hudgeba778 Sep 07 '19

Gotta put in a failsafe

17

u/Crimsonshore Sep 07 '19

SafeFail™️

30

u/John_Mansell Sep 07 '19

Wow, everything was going fine until...

12

u/caguiclajmg Sep 07 '19

I used to do a similar thing when I was working with a janky GSM module for an embedded project. Just let the (watch)dog bite your ass and everything is hunky-dory.

5

u/Use_My_Body Sep 07 '19

Mmm, I think I'll try to get the (watch)dog to knot my ass instead~

1

u/nate998877 Sep 15 '19

username checks out

27

u/heyf00L Sep 07 '19

I learned recently that infinite loops are undefined behavior in C++. So who knows what this does.

11

u/[deleted] Sep 07 '19

The current form is UB but I believe that while(I) { ... } is legal bc it has observable effects.