Main Content

CERT C++: CON41-C

Wrap functions that can fail spuriously in a loop

Description

Rule Definition

Wrap functions that can fail spuriously in a loop.1

Polyspace Implementation

The rule checker checks for Function that can spuriously fail not wrapped in loop.

Examples

expand all

Issue

Function that can spuriously fail not wrapped in loop occurs when the following atomic compare and exchange functions that can fail spuriously are called from outside a loop.

  • C atomic functions:

    • atomic_compare_exchange_weak()

    • atomic_compare_exchange_weak_explicit()

  • C++ atomic functions:

    • std::atomic<T>::compare_exchange_weak(T* expected, T desired)

    • std::atomic<T>::compare_exchange_weak_explicit(T* expected, T desired, std::memory_order succ, std::memory_order fail)

    • std::atomic_compare_exchange_weak(std::atomic<T>* obj, T* expected, T desired)

    • std::atomic_compare_exchange_weak_explicit(volatile std::atomic<T>* obj, T* expected, T desired, std::memory_order succ, std::memory_order fail)

The functions compare the memory contents of the object representations pointed to by obj and expected. The comparison can spuriously return false even if the memory contents are equal. This spurious failure makes the functions faster on some platforms.

Risk

An atomic compare and exchange function that spuriously fails can cause unexpected results and unexpected control flow.

Fix

Wrap atomic compare and exchange functions that can spuriously fail in a loop. The loop checks the failure condition after a possible spurious failure.

Example — atomic_compare_exchange_weak() Not Wrapped in Loop
#include <atomic>
#include <stdbool.h>
using namespace std;
static atomic<bool> flag(false);
void toggle_flag(void)
{
	bool old_flag = atomic_load(&flag);
	bool new_flag;
	if (!atomic_compare_exchange_weak(&flag, &old_flag, new_flag)){//Noncompliant
		new_flag = !old_flag;
	}

}

bool get_flag(void)
{
	return atomic_load(&flag);
}

In this example, the functiontoggle_flag uses atomic_compare_exchange_weak() to compare flag and old_flag. If the variables are identical, flag is toggles to new_flag. When atomic_compare_exchange_weak() fails spuriously, the flag is toggled unnecessarily.

Correction — Wrap atomic_compare_exchange_weak() in a do-while Loop

One possible correction is to wrap the call to atomic_compare_exchange_weak() in a while loop. The loop checks the failure condition after a possible spurious failure.

#include <atomic>
#include <stdbool.h>
using namespace std;
static atomic<bool> flag(false);


void toggle_flag(void)
{
  bool old_flag = atomic_load(&flag);
  bool new_flag;
  do {
    new_flag = !old_flag;
  } while (!atomic_compare_exchange_weak(&flag, &old_flag, new_flag));

}

bool get_flag(void)
{
    return atomic_load(&flag);
}

Check Information

Group: 10. Concurrency (CON)

Version History

Introduced in R2019a


1 This software has been created by MathWorks incorporating portions of: the “SEI CERT-C Website,” © 2017 Carnegie Mellon University, the SEI CERT-C++ Web site © 2017 Carnegie Mellon University, ”SEI CERT C Coding Standard – Rules for Developing safe, Reliable and Secure systems – 2016 Edition,” © 2016 Carnegie Mellon University, and “SEI CERT C++ Coding Standard – Rules for Developing safe, Reliable and Secure systems in C++ – 2016 Edition” © 2016 Carnegie Mellon University, with special permission from its Software Engineering Institute.

ANY MATERIAL OF CARNEGIE MELLON UNIVERSITY AND/OR ITS SOFTWARE ENGINEERING INSTITUTE CONTAINED HEREIN IS FURNISHED ON AN "AS-IS" BASIS. CARNEGIE MELLON UNIVERSITY MAKES NO WARRANTIES OF ANY KIND, EITHER EXPRESSED OR IMPLIED, AS TO ANY MATTER INCLUDING, BUT NOT LIMITED TO, WARRANTY OF FITNESS FOR PURPOSE OR MERCHANTABILITY, EXCLUSIVITY, OR RESULTS OBTAINED FROM USE OF THE MATERIAL. CARNEGIE MELLON UNIVERSITY DOES NOT MAKE ANY WARRANTY OF ANY KIND WITH RESPECT TO FREEDOM FROM PATENT, TRADEMARK, OR COPYRIGHT INFRINGEMENT.

This software and associated documentation has not been reviewed nor is it endorsed by Carnegie Mellon University or its Software Engineering Institute.