Zum Hauptinhalt springen
AI & KI NachrichtenTheAIGRID: DeepSeek Just Made American AI Companies Panic(28.10.2026 um 07:00 Uhr)
•
YouTube Security VideosGoogle Cloud Tech: What "one agent, everywhere you work" actually means(28.10.2026 um 00:00 Uhr)
•
Windows Tipps & SecurityMicrosoft warns AI agents "can't be trusted" as adoption grows(22.10.2026 um 17:49 Uhr)
••••
Unix & Linux ServerDistribution Release: Maze Linux 2026.10.26(26.10.2026 um 17:34 Uhr)
•
Unix & Linux ServerSecurity: Mangelnde Rechteprüfung in proftpd (Slackware)(28.10.2026 um 07:05 Uhr)
•
Linux Tipps & HardeningSecurity: Denial of Service in libinstpatch (Fedora)(28.10.2026 um 07:05 Uhr)
•
Linux Tipps & HardeningSecurity: Pufferüberlauf in rust-onefetch (Fedora)(28.10.2026 um 07:08 Uhr)
•
AI & KI NachrichtenTheAIGRID: DeepSeek Just Made American AI Companies Panic(28.10.2026 um 07:00 Uhr)
•
YouTube Security VideosGoogle Cloud Tech: What "one agent, everywhere you work" actually means(28.10.2026 um 00:00 Uhr)
•
Windows Tipps & SecurityMicrosoft warns AI agents "can't be trusted" as adoption grows(22.10.2026 um 17:49 Uhr)
••••
Unix & Linux ServerDistribution Release: Maze Linux 2026.10.26(26.10.2026 um 17:34 Uhr)
•
Unix & Linux ServerSecurity: Mangelnde Rechteprüfung in proftpd (Slackware)(28.10.2026 um 07:05 Uhr)
•
Linux Tipps & HardeningSecurity: Denial of Service in libinstpatch (Fedora)(28.10.2026 um 07:05 Uhr)
•
Linux Tipps & HardeningSecurity: Pufferüberlauf in rust-onefetch (Fedora)(28.10.2026 um 07:08 Uhr)
•
Intelligence View
⚡ tsecurity.de Intelligence

GTest and short-circuit evaluation in C++

What is short-circuit evaluation in C++? Short-circuit evaluation is a behavior of logical operators in C++ where the second part of an expression is evaluated…

Beitrag
0
Seite
0
↗ Quelle (dev.to)
Social ReaktionenReagiere als Erste:r — dein Feedback zählt!




What is short-circuit evaluation in C++?



Short-circuit evaluation is a behavior of logical operators in C++ where the second part of an expression is evaluated only if it is necessary to determine the final result.



For example, in an expression using the logical OR operator, if the left-hand side already evaluates to true, the right-hand side is never executed because the entire expression must already be true.




if (is_admin || hasPermission())
{
// ...
}






If is_admin is true, the function hasPermission() will never be called. The same rule applies to the logical AND operator in the opposite direction: if the left-hand side is false, the right-hand side is skipped. Such behavior is often beneficial because it avoids unnecessary work.






How does this matter in practice?



Short-circuit evaluation becomes especially important in unit tests when mocking calls used as parts of if statements. Consider the following code:




class PermissionService
{
public:
virtual bool HasPermission() = 0;
};

bool CanAccess(bool is_admin, PermissionService& service)
{
if (is_admin && service.HasPermission())
{
return true;
}

return false;
}






At first glance, one of the unit tests testing the above code could look like this:




class MockPermissionService : public PermissionService
{
public:
MOCK_METHOD(bool, HasPermission, (), (override));
};

TEST(CanAccessTest, ReturnsTrueForAdmin)
{
StrictMock<MockPermissionService> service;

EXPECT_CALL(service, HasPermission()).WillOnce(Return(true));
EXPECT_FALSE(CanAccess(false, service));
}






However, when you run this test, you'll see that it failed with the following error:




Actual function call count doesn't match EXPECT_CALL(service, HasPermission())...
Expected: to be called once
Actual: never called - unsatisfied and active






How is this possible that HasPermission() was never called if we clearily see it in the tested code? The answer is: short-circuit evaluation.



Since is_admin is false, C++ short-circuit evaluation stops the expression immediately. The call to service.HasPermission() is never executed and as a result the mocked method is never called, so the EXPECT_CALL expectation is not satisfied.



The failure is not caused by incorrect production code. The issue is that the test assumes both sides of the condition are evaluated, while the language guarantees that only the necessary side is executed.



The correct test should reflect the actual execution path:




TEST(CanAccessTest, ReturnsTrueForAdmin)
{
StrictMock<MockPermissionService> service;

EXPECT_TRUE(CanAccess(false, service));
}






Or if we specifically want to verify the permission check path, we should make the first condition evaluate to false:




bool CanAccess(bool is_admin, PermissionService& service)
{
if (service.HasPermission() && is_admin)
{
return true;
}

return false;
}


Intelligence Digest gratis Dagelijks het belangrijkste beveiligingsnieuws · kan op elk moment worden opgehaald
Community Rating & Social Proof
⭐ Leser-Wertung
–
Ø / 5.0
Nog geen recensies van lezers
Was deze pagina nuttig?
0
Jetzt Bewertung abgeben
🟢 Niedrig (1-3.9) 🟡 Mittel (4.0-6.9) 🔴 Hoch (7.0-8.9) Kritisch (9.0-10.0)

Verwant Story Cluster & Sources (vector AI)

Soortgelijke bijdragen
🔍 Verwandte News

Auch interessante Nachrichten GTest and short-circuit evaluation in C++

Thematisch verwandte Begriffe: GTest, shortcircuit, evaluation · 6 Treffer

Laden...

Video's zijn geladen...

Laden...

Bijdragen zijn geladen ...

Laden...

Video's zijn geladen...

Laden...

Bijdragen zijn geladen ...

Laden...

Video's zijn geladen...

Laden...

Bijdragen zijn geladen ...

Laden...

Video's zijn geladen...

💬 Reacties worden geladen…
Trek om te vernieuwen
Nächster Beitrag