﻿ V3093. The operator evaluates both operands. Perhaps a short-circuit operator should be used instead. # V3093. The operator evaluates both operands. Perhaps a short-circuit operator should be used instead.

The analyzer detected a possible error that has to do with the programmer confusing operator '&' with '&&' or '|' with '||' when using them to form a logical expression.

Conditional operators AND ('&&') / OR ('||') evaluate the second operand only when necessary (see Short circuit) while operators '&' and '|' always evaluate both operands. It is very likely that the code author did not intend it to work that way.

Consider the following example:

``````if ((i < a.m_length) & (a[i] % 2 == 0))
{
sum += a[i];
}``````

Suppose the 'a' object is a container; the number of elements in it is stored in the 'm_length' member. We need to find the sum of even elements, making sure that we do not go beyond the array boundaries.

Because of a typo, our example uses operator '&' instead of '&&'. It will result in an array-index-out-of-bounds error when evaluating the '(a[i] % 2 == 0)' subexpression if index 'i' appears to be greater than or equal to 'a.m_length'. Regardless of whether the left part of the expression is true or false, the right part will be evaluated anyway.

Fixed code:

``````if ((i < a. m_length) && (a[i] % 2 == 0))
{
sum += a[i];
}``````

Here is another example of incorrect code:

``````if (x > 0 | BoolFunc())
{
....
}``````

The call to the 'BoolFunc()' function will execute all the time, even when the '(x > 0)' condition is true.

Fixed code:

``````if (x > 0 || BoolFunc())
{
....
}``````

Code fragments detected by diagnostic V3093 do not always contain errors, but they do deal with expressions that are non-optimal from the viewpoint of performance (especially when they use calls to complex functions).

If, however, the conditional expression is correct and written as you intended, you can mark this fragment with special comment "//-V3093" so that the analyzer does not output the warning:

``````if (x > 0 | BoolFunc()) //-V3093
{
....
}``````

This diagnostic is classified as:

 CWE-480
 You can look at examples of errors detected by the V3093 diagnostic.

418
14 302

### Do you make errors in the code?
{"ft_lang_label":"__label__en","ft_lang_prob":0.8864434,"math_prob":0.9539014,"size":2618,"snap":"2021-04-2021-17","text_gpt3_token_len":603,"char_repetition_ratio":0.08722265,"word_repetition_ratio":0.03956044,"special_character_ratio":0.2734912,"punctuation_ratio":0.1362764,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9808536,"pos_list":[0,1,2,3,4,5,6],"im_url_duplicate_count":[null,null,null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-01-21T02:00:27Z\",\"WARC-Record-ID\":\"<urn:uuid:953e234c-577f-4e41-a50e-11daef4c4606>\",\"Content-Length\":\"74640\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:590408ad-5eb4-46bc-b1f7-c2d23bcf3a18>\",\"WARC-Concurrent-To\":\"<urn:uuid:11c2058a-b69c-4bec-bc1b-1b65cc64942e>\",\"WARC-IP-Address\":\"94.249.192.186\",\"WARC-Target-URI\":\"https://www.viva64.com/en/w/v3093/\",\"WARC-Payload-Digest\":\"sha1:I3KDJES3I7ZQJAKOYVJKQYHCWCFLXJ5Y\",\"WARC-Block-Digest\":\"sha1:LI4DMC2UOOG7B7WTO6T7ME3YM3OSJT2I\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-04/CC-MAIN-2021-04_segments_1610703522150.18_warc_CC-MAIN-20210121004224-20210121034224-00295.warc.gz\"}"}