Skip to content

[Guideline] Add do not divide by 0 #132

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions src/coding-guidelines/expressions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,44 @@ Expressions
}

fn with_base(_: &Base) { ... }

.. guideline:: Do not divide by 0
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Asking a bit of a practical question to folks. Does the combination of:

  • mandatory
  • undecidable

put too great of a burden on reviewers / auditors or is this "just the way it is and should be" given the nature of writing safety-critical code?

Tagging @AlexCeleste and @rcseacord for their thoughts as well.

:id: gui_kMbiWbn8Z6g5
:category: Mandatory
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
:category: Mandatory
:category: mandatory

Should be lowercase, see here in conf.py.

:status: draft
:release: latest
:fls: fls_Q9dhNiICGIfr
:decidability: Undecidable
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
:decidability: Undecidable
:decidability: undecidable

Should be lowercase, see here in conf.py.

:scope: System
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
:scope: System
:scope: system

Should be lowercase, see here in conf.py.

:tags: numerics

This guideline applies when unsigned integer or two’s complement division is performed. This includes the
evaluation of a remainder expression.

.. rationale::
:id: rat_h84NjY2tLSBW
:status: draft

Integer division by zero results in a panic, which is an abnormal program state and may terminate the process.

.. non_compliant_example::
:id: non_compl_ex_LLs3vY8aGz0F
:status: draft

When the division is performed, the right operand is evaluated to zero and the program panics.

.. code-block:: rust

let x = 0;
let x = 5 / x;

.. compliant_example::
:id: compl_ex_Ri9pP5Ch3kbb
:status: draft

There is no compliant way to perform integer division by zero. A checked division will prevent any
division by zero from happening. The programmer can then handle the returned Option.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check out how we can use one of the nice bits of the coding guidelines extension to link to the Rust standard library.

Suggested change
division by zero from happening. The programmer can then handle the returned Option.
division by zero from happening. The programmer can then handle the returned :std:``std::option::Option``.

(This was shamelessly stolen from the FLS extension)


.. code-block:: rust

let x = 5u8.checked_div(0);