天天看點

C++核心準則​Pro.bounds:邊界安全群組

Pro.bounds: Bounds safety profile

Pro.bounds:邊界安全群組

This profile makes it easier to construct code that operates within the bounds of allocated blocks of memory. It does so by focusing on removing the primary sources of bounds violations: pointer arithmetic and array indexing. One of the core features of this profile is to restrict pointers to only refer to single objects, not arrays.

此規則群組使建構在配置設定的記憶體塊範圍内運作的代碼更加容易。它通過專注于消除違背邊界規則的主要來源來做到這一點:指針算術和數組索引。此規則群組的核心功能之一是将指針限制為僅引用單個對象,而不是數組。

We define bounds-safety to be the property that a program does not use an object to access memory outside of the range that was allocated for it. Bounds safety is intended to be complete only when combined with Type safety and Lifetime safety, which cover other unsafe operations that allow bounds violations.

我們将邊界安全性定義為程式不通過對象通路為其配置設定的範圍之外的記憶體的屬性。僅當與類型安全性和生命周期安全性結合使用時,邊界安全性才是完整的,後者包含允許違反邊界的其他不安全操作。

Bounds safety profile summary:

邊界安全群組概要:

  • Bounds.1: Don't use pointer arithmetic. Use span instead: Pass pointers to single objects (only) and Keep pointer arithmetic simple.

    Bound.1:不要使用指針算法。改用span:​​将指針傳遞給單個對象(僅)​​,并​​使指針的運算保持簡單​​。

  • Bounds.2: Only index into arrays using constant expressions: Pass pointers to single objects (only) and Keep pointer arithmetic simple.

    Bounds.2:僅使用常量表達式對數組進行索引:​​(僅)将指針傳遞給單個對象​​,并​​使指針的運算保持簡單​​。

  • Bounds.3: No array-to-pointer decay: Pass pointers to single objects (only) and Keep pointer arithmetic simple.

    Bounds.3:沒有數組到指針的退化:​​将指針傳遞給單個對象(僅)​​,并​​使指針的運算保持簡單​​。

  • Bounds.4: Don't use standard-library functions and types that are not bounds-checked: Use the standard library in a type-safe manner.

    Bounds.4:不要使用未經邊界檢查的标準庫函數和類型:​​以類型安全的方式使用标準庫​​。

Impact(影響)

Bounds safety implies that access to an object - notably arrays - does not access beyond the object's memory allocation. This eliminates a large class of insidious and hard-to-find errors, including the (in)famous "buffer overflow" errors. This closes security loopholes as well as a prominent source of memory corruption (when writing out of bounds). Even if an out-of-bounds access is "just a read", it can lead to invariant violations (when the accessed isn't of the assumed type) and "mysterious values."

邊界安全性意味着對對象(尤其是數組)的通路不會超出對象的記憶體配置設定範圍。這消除了許多隐患和難以發現的錯誤,包括(著名的)“緩沖區溢出”錯誤。這可以消除安全漏洞以及記憶體損壞的主要根源(超出限制時)。即使越界通路隻是“讀取”,它也可能導緻違反不變量(當通路的類型不是假定的類型時)和“神秘的價值”。

原文連結

​​https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#probounds-bounds-safety-profile​​

新書介紹

​​《實戰Python設計模式》​​是作者最近出版的新書,拜托多多關注!

C++核心準則​Pro.bounds:邊界安全群組

本書利用Python 的标準GUI 工具包tkinter,通過可執行的示例對23 個設計模式逐個進行說明。這樣一方面可以使讀者了解真實的軟體開發工作中每個設計模式的運用場景和想要解決的問題;另一方面通過對這些問題的解決過程進行說明,讓讀者明白在編寫代碼時如何判斷使用設計模式的利弊,并合理運用設計模式。

對設計模式感興趣而且希望随學随用的讀者通過本書可以快速跨越從了解到運用的門檻;希望學習Python GUI 程式設計的讀者可以将本書中的示例作為設計和開發的參考;使用Python 語言進行圖像分析、資料處理工作的讀者可以直接以本書中的示例為基礎,迅速建構自己的系統架構。

覺得本文有幫助?請分享給更多人。

關注微信公衆号【面向對象思考】輕松學習每一天!

面向對象開發,面向對象思考!

繼續閱讀