TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Ask HN: What are some good resources to learn safety-critical C/C++ coding from?

42 点作者 bobowzki将近 3 年前
I will need to learn about writing safety-critical C&#x2F;C++ code at my current job. Many resources[1-2] tell you what not to do, but few tell you what to do[3].<p>What are some excellent examples of open source code bases from which to learn?<p>1: https:&#x2F;&#x2F;www.misra.org.uk&#x2F; 2: https:&#x2F;&#x2F;yurichev.com&#x2F;mirrors&#x2F;C&#x2F;JPL_Coding_Standard_C.pdf 3: https:&#x2F;&#x2F;nasa.github.io&#x2F;fprime&#x2F;UsersGuide&#x2F;dev&#x2F;code-style.html

18 条评论

ncmncm将近 3 年前
First, stop saying C&#x2F;C++. If you are talking about C, you are not talking about C++. If you are talking about C++, you are not talking about C.<p>Second, give up on C. It simply has not got the resources to help you with safety. It is a wholly lost cause.<p>In C++, you can package semantics in libraries in ways hard to misuse accidentally. In effect, your library provides the safety that Rust reserves to its compiler. C++ offers more power to the library writer than Rust offers. Use it!
评论 #32364345 未加载
maldev将近 3 年前
CERT C is a really good standard and book. But there&#x27;s really no reason to read a book. It&#x27;s very simple if you follow these steps.<p>Step 1. NO CONSTANT NUMBERS! All constants should be a define macro or a constant. This will allow you to change code without overflows and having to update the number in 20 places and not knowing what number to use when looping through.<p>Step 2. SESE(RAII in c++, but most use SESE even in c++). SINGLE ENTRY SINGLE EXIT. Your code should look like<p>&quot; int *ptr = foo(); if(ptr == nullptr) DEBUG_PRINT(&quot;FAILED ALLOCATING PTR IN __FILE__ @ __LINE__) goto exit;<p>EXIT: if(ptr) free(ptr); .... &quot;<p>So any allocations you cleanup in exit. This way you won&#x27;t miss it with wierd control flows. This is reccomended by all cert c standards.<p>Step 3: If you can, there&#x27;s analyzers you can use that will point out all bugs by annotating your code. SAL is arguable the best in the industry and you can catch pretty much all bugs.<p>Step 4: Even without an analyzer, you should be looking at all warnings and either adding a compiler macro to ignore it, or fixing whats causing it.
评论 #32363691 未加载
评论 #32363064 未加载
gooddelta将近 3 年前
ITT: People who don&#x27;t understand safety-critical systems telling people how to write safety-critical systems.<p>The most popular answer in this thread is &quot;you can only write safe C++&quot; which is bullshit. The language that you use will likely be dictated by the toolchain you&#x27;re forced to use to meet whatever standard your org has adopted. For example, if you&#x27;re in the automotive realm and following something like ISO-26262, you&#x27;ll only be able to use a qualified toolchain that&#x27;s compatible with your safety MCU – so you&#x27;ll likely be limited to C or C++, and then FURTHER limited by MISRA standards to a subset of those languages. There is no version of Rust that may be used for safety-critical systems, currently – despite the fact that it&#x27;s arguably a better language, the rigorous verification&#x2F;documentation work hasn&#x27;t been done yet. If you&#x27;re looking for an alternative to C or C++ for use in safety-critical domains, look at Ada.<p>You will likely not find any example of an open source codebase for safety critical systems. Rigorously-developed safety-critical systems cost millions of dollars to produce, document, run through V&amp;V, etc. They don&#x27;t tend to get released as OSS.<p>For the rest of the folks in this thread: type safety, memory safety, etc. are awesome features – but having a language with these features doesn&#x27;t allow you to build a safety-critical system. It doesn&#x27;t even begin to. If you&#x27;re curious, you can start to look at the roadmap for the Ferrocene project – the company behind it is working with the folks from AdaCore (AFAICR?) to make a version of Rust for safety-critical systems a reality (one that I&#x27;m very much looking forward to!)
oumua_don17将近 3 年前
This book `Embracing Modern C++ safely` just showed up in my book feed, you may find it useful. [1] is a review of the book.<p>[1] <a href="https:&#x2F;&#x2F;www.cppstories.com&#x2F;2022&#x2F;embracing-modern-cpp-book&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.cppstories.com&#x2F;2022&#x2F;embracing-modern-cpp-book&#x2F;</a>
wcunning将近 3 年前
Find the industry standards you&#x27;re supposed to follow. If your job requires safety compliant code, the company should have documents that give good style guides. As mentioned by other commenters, aviation has its own standards, and you linked to some of the NASA work.<p>In automotive, where I&#x27;ve done ISO26262 work (Functional Safety standards), there are MISRA and Cert C static checkers and guidelines to make them not scream too much, not to mention the fact that you&#x27;ll be following the style of the code you modify. Beyond that, you can find the industry guidelines for whatever standards you&#x27;re responsible to follow. It gets worse as you get more strict -- brake controller code in the safety critical path has to meet the strictest formal methods checking as well as a bunch of in-use, on-controller testing. Generally, no one gets thrown into that without any training on the grounds of safety and liability alone.
评论 #32364153 未加载
physPop将近 3 年前
From Stroustrup himself (consulted on guidelines for the F-35). <a href="https:&#x2F;&#x2F;www.stroustrup.com&#x2F;JSF-AV-rules.pdf" rel="nofollow">https:&#x2F;&#x2F;www.stroustrup.com&#x2F;JSF-AV-rules.pdf</a><p>Maybe stricter than you&#x27;re looking for, but no memory is allocated or deallocated after the plane takes off and until it lands!
_448将近 3 年前
Here is another one I came across: <a href="https:&#x2F;&#x2F;www.autosar.org&#x2F;fileadmin&#x2F;user_upload&#x2F;standards&#x2F;adaptive&#x2F;17-03&#x2F;AUTOSAR_RS_CPP14Guidelines.pdf" rel="nofollow">https:&#x2F;&#x2F;www.autosar.org&#x2F;fileadmin&#x2F;user_upload&#x2F;standards&#x2F;adap...</a>
jbms将近 3 年前
Useful resources: colleagues, professional training, case studies of errors.<p>If your job is safety critical software I guess they&#x27;d pay for relevant training. If not, looking at the course outlines at least lets you know what trainers think are important topics, for example<p><a href="https:&#x2F;&#x2F;www.feabhas.com&#x2F;content&#x2F;robust-software-embedded-systems-1" rel="nofollow">https:&#x2F;&#x2F;www.feabhas.com&#x2F;content&#x2F;robust-software-embedded-sys...</a><p>One training course I had talked about how to design a system with integrity while integrating open source code of unknown integrity. Since software quality and safety critical software depends so much on process, then open source by default isn&#x27;t built to any integrity level. If a system needs two independent implementations of a calculation, an open source code base would never show that.<p>If you have an experienced safety engineer, ask them about how typically to design the system and software to make the safety case easier and they&#x27;ll have some ideas of what needs to commonly be done. It depends on the integrity level what strategy and process needs to be followed.<p>It&#x27;s not just the code style, but there&#x27;s a broader mindset that you need to develop.<p>There&#x27;s also good presentations and lectures that come up from time to time here or on YouTube where the failure of safety critical software is studied. These can be excellent case studies: Such as: <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=31236303" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=31236303</a>
buescher将近 3 年前
Not open source, but Medtronic published a complete ventilator design and documentation, including firmware, in response to the COVID crisis.
评论 #32359674 未加载
rgauto将近 3 年前
As others have mentioned start with identifing the relevant functional safety standards for your industry. IEC 61508-3 and the annexes, whilst very verbose, is basically the textbook for safety development.<p>Pro tip, standards can be hard to find and expensive but you can rent or buy them cheaply from the Latvian Standards website (<a href="https:&#x2F;&#x2F;www.lvs.lv&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.lvs.lv&#x2F;</a>), most are harmonised and exactly the same as IEC or ISO parent standards, just with an LVS cover sheet.<p>This book ,Embedded Software Development for Safety-Critical Systems by Chris Hobbs gives a great overview of safety software development in general and the key standards, I found it easy to read.<p><a href="https:&#x2F;&#x2F;www.routledge.com&#x2F;Embedded-Software-Development-for-Safety-Critical-Systems-Second-Edition&#x2F;Hobbs&#x2F;p&#x2F;book&#x2F;9780367338855" rel="nofollow">https:&#x2F;&#x2F;www.routledge.com&#x2F;Embedded-Software-Development-for-...</a><p>On a practical note if using C or C++ get familiar with commonly used language subsets such as MISRA (<a href="https:&#x2F;&#x2F;www.misra.org.uk" rel="nofollow">https:&#x2F;&#x2F;www.misra.org.uk</a>) or CERT C, again which is more relevant will depend on industry.<p>Gimpel&#x27;s PC-Lint is a commonly used static analyser for MISRA compliance, and you can try with it on their website (<a href="https:&#x2F;&#x2F;gimpel.com&#x2F;demo.html" rel="nofollow">https:&#x2F;&#x2F;gimpel.com&#x2F;demo.html</a>), I haven&#x27;t come across a free tool complete checker but you can do a lot with clang and GCC.<p>Some mention of Rust here but I think that would be a hard language to get through a certification process due to the limited options for qualified tools. That said there is work being done there, <a href="https:&#x2F;&#x2F;ferrous-systems.com&#x2F;ferrocene" rel="nofollow">https:&#x2F;&#x2F;ferrous-systems.com&#x2F;ferrocene</a>
jbh1将近 3 年前
SEI Cert C coding standard is still updated and has good advice <a href="https:&#x2F;&#x2F;wiki.sei.cmu.edu&#x2F;confluence&#x2F;plugins&#x2F;servlet&#x2F;mobile?contentId=87152044#content&#x2F;view&#x2F;87152044" rel="nofollow">https:&#x2F;&#x2F;wiki.sei.cmu.edu&#x2F;confluence&#x2F;plugins&#x2F;servlet&#x2F;mobile?c...</a>
charcircuit将近 3 年前
Architect your system for handling failures. No software will be bug free, because the hardware you run it is not perfect and can introduce things like bit flips. It&#x27;s okay to fail, but you need to be be able to recover.
评论 #32363992 未加载
sudopluto将近 3 年前
i like this one: <a href="http:&#x2F;&#x2F;isocpp.github.io&#x2F;CppCoreGuidelines&#x2F;CppCoreGuidelines" rel="nofollow">http:&#x2F;&#x2F;isocpp.github.io&#x2F;CppCoreGuidelines&#x2F;CppCoreGuidelines</a>
评论 #32363847 未加载
throwaway81523将近 3 年前
dwheeler.com and adacore.com are good places to look. Even though the latter is an Ada site, you can learn things from it. Why are you stuck using C and&#x2F;or C++ anyway? And what is your application? That affects the answer.<p>I agree with the posters who emphasize that C and C++ are not similar languages and shouldn&#x27;t be lumped together, fwiw.
stephenbennyhat将近 3 年前
Start with &quot;Safer C&quot; by Les Hatton. It is getting on a bit now but still a worthwhile read.
speedgoose将近 3 年前
I will be this guy, but can’t you use a better more safe programming language and expose an API for C?
评论 #32359453 未加载
评论 #32359058 未加载
评论 #32363837 未加载
评论 #32359215 未加载
blastonico将近 3 年前
How to program C++ - Deitel &amp; Deitel
matt3210将近 3 年前
Write MISRA compliant and you good!!
评论 #32359807 未加载