# How PVS-Studio prevents rash code changes, example N4

![Blender, PVS-Studio, std::clamp](https://habrastorage.org/r/w1560/webt/u3/gu/za/u3guzacb7qda9odb8hw_wtowxaq.png)  

If you regularly use a static code analyzer, you can save time on guessing why the new code doesn't work as planned. Let's look at another interesting error — the function broke during refactoring, and no one noticed that. No one — except for PVS-Studio that can automatically scan the project and email the report to us.

It's the fourth small note illustrating how quickly [PVS-Studio](https://pvs-studio.com/en/pvs-studio/) finds errors in the new code. I thought of taking a break from it. But when I saw PVS-Studio's report about [Blender](https://github.com/blender/blender) in my emails, I discarded this thought. Let me show you another error, simple and beautiful at the same time.

Once upon a time there was code that processed a vector of values. It prevented values from going beyond a certain range.

```
#define CLAMP(a, b, c) \
  { \
    if ((a) < (b)) { \
      (a) = (b); \
    } \
    else if ((a) > (c)) { \
      (a) = (c); \
    } \
  } \
  (void)0

template  inline T
clamp(const T &a, const bT &min\_v, const bT &max\_v)
{
 T result = a;
 for (int i = 0; i < T::type\_length; i++) {
 CLAMP(result[i], min\_v, max\_v);
 }
 return result;
}
```

Everything was good. And then the developer decided to abandon the custom *CLAMP* macro and use the standard *[std::clamp](https://en.cppreference.com/w/cpp/algorithm/clamp)* function. And the [commit](https://github.com/blender/blender/commit/399168f3c13fadb41c9fbec8a1b5c56cb6609343) that supposed to make the code better looked like this:

```
template 
inline vec\_base
 clamp(const vec\_base &a, const T &min, const T &max)
{
 vec\_base result = a;
 for (int i = 0; i < Size; i++) {
 std::clamp(result[i], min, max);
 }
 return result;
}
```

It seems like the developer was in a hurry. Do you see the error? Maybe yes, maybe no. Anyway, the developer who wrote the code, didn't notice that it was broken.

But the all-seeing PVS-Studio static analyzer warns us immediately:

[CWE-252] [V530](https://pvs-studio.com/en/docs/warnings/v530/): The return value of function 'clamp' is required to be utilized. BLI\_math\_vector.hh 88

The point being — the *[std::clamp](https://en.cppreference.com/w/cpp/algorithm/clamp)* function doesn't change the value of the element in the container:

```
template 
constexpr const T&
clamp( const T& v, const T& lo, const T& hi );
```

The *CLAMP* macro used to change the value, but the standard function did not. Now the code is broken and is waiting for someone to notice an error and look for its cause. With PVS-Studio, the developers could have found and fixed this error at the code writing stage. Using static analysis regularly, you can save your time and resources.

Note. By the way, there's another incorrect use of *std::clamp* in the code.

The correct version of code: 

```
template 
inline vec\_base
clamp(const vec\_base &a, const T &min, const T &max)
{
 vec\_base result = a;
 for (int i = 0; i < Size; i++) {
 result[i] = std::clamp(result[i], min, max);
 }
 return result;
}
```

Thank you for your time. And drop by to read about the [top 10 bugs](https://pvs-studio.com/en/blog/posts/cpp/0901/) found in C++ open-source projects in 2021.