Weerawad Ruangjaroon, Dreamstime.com
Programmer Promo

Good Code Means Secure Code

Sept. 15, 2020
Good, clean code is the starting point for secure software, and having the right tools is essential when that’s not the case.

No one wants to write bad, buggy code, but that’s often what we get from developers who don’t follow best practices. This problem is worse when one considers the requirements for safe and secure applications. These days, safe and secure code covers just about everything. If you’re sharing the code, then people depend on its quality. If you’re designing a product for self-driving cars, aircraft, or medical devices, then it’s even more important to use the right tools and procedures to deliver reliable software that’s also secure.

I do recommendAda and SPARKfor programming in general because of features likecontract-based programming. However, I know C still dominates for embedded programmers. For those who haven’t moved to C++, I would push that as a better solution even if object-oriented programming (OOP) isn’t part of your repertoire. C++ is significantly better when it comes to everything from namespaces to type management to references. The new C++20 standard will incorporate new features like modules.

Many assume C++ is less efficient because C and C++ have more overhead. That’s not really the case, but if you use some features like virtual class functions, then, yes, there’s overhead. It’s on par with any overhead of providing similar functionality in C, although in a much cruder fashion. Using C++ features like non-virtual methods in a class allows for the same efficiency as C while enabling methods to be hidden as well.

So, what does code quality have to do with safety and security?

It’s difficult to have either without good code to back them up. Thoughcoding standards can helpin writing good code, it’s just a starting point. Code reviews can also help, but many developers overlook tools like static analysis that have the advantage of enforcing rules by a computer, which brings a level of consistency to the process as well as enforcement.

One well-known standard is MISRA C. It started in the automotive space where it remains important, butMISRA C and MISRA C++ are applicable to any application. The standards include a multitude of rules that limit the functionality available within the programming language, as well as how features are used. Some, like Rule 1.3, address undefined behavior, which covers a lot of ground when it comes to C. The C90/C99 standard has over 200 instances of undefined behavior.

等MISRA - C规则可能看上去不那么有用as preventing local variable and type names from hiding more global definitions. This can result in a debugging nightmare if one isn’t aware, because he or she may be looking at the more global definition and wondering why it’s not operating as expected.

I’ve been working withIAR Systems’Embedded Workbench, which has MISRA C support built-in, along withGigaDevice的GD32V RISC-Vboard. It was an interesting exercise getting the demo code working. Turning on MISRA C with the default settings results in hundreds of errors, but disabling or tweaking a few rules cuts this number down to a dozen, so it’s worth making the code changes.

Following the rules when writing code and having the compiler check it doesn’t eliminate all errors. However, it can force better programming practices and highlights errors that would otherwise be overlooked until the code was running in the field. Debugging costs rise exponentially as it moves farther from the original developer.

Developing safe and secure code should be part of the goals for any developer. Using the right procedures and tools can help, especially for larger projects and groups.

From Our Partners

Welcome to The Edge

As the cost of embedded networked devices falls—consider the Raspberry Pi as one example—they become ubiquitous. But, a hidden cost in this prolifera…

USB Type-C® & USB Power Delivery

Our broad offering of USB Type-C® devices and PD controllers for USB PD capability offers the flexibility and integration required to design and impl…

ULTRA-HIGH RELIABILITY & LOW LATENCY

As you read this, 5G is rolling out across the United States.. Some people have a 5G compatible phone that can connect to an AT&T network, T-Mobile, o…

2.2-Mhz, Low EMI, Dual 3-A USBType-C® Charging Ports Controller

2.2 mhz, low-EMI,双,3-A USB Type-C charging ports converter with thermal management

Voice your opinion!

This site requires you to register or login to post a comment.
No comments have been added yet. Want to start the conversation?

From Our Partners

Welcome to The Edge

As the cost of embedded networked devices falls—consider the Raspberry Pi as one example—they become ubiquitous. But, a hidden cost in this prolifera…

USB Type-C® & USB Power Delivery

Our broad offering of USB Type-C® devices and PD controllers for USB PD capability offers the flexibility and integration required to design and impl…

ULTRA-HIGH RELIABILITY & LOW LATENCY

As you read this, 5G is rolling out across the United States.. Some people have a 5G compatible phone that can connect to an AT&T network, T-Mobile, o…
2018beplay

1-Hz, 8-Bit RISC Micro Built Using Minecraft Redstone

Dec. 24, 2021
The CHUNGUS 2 is an 8-bit micro with features such as a 4-stage pipeline.
alt.embedded

The Complexities of Electronic Design and Scheduling

Dec. 17, 2021
Editor Bill Wong comments on the challenges faced by electronic developers these days.
Baidu