Reputation: 321
We are compiling and running user submitted C++ programs in a sandbox environment with the following compile command:
g++ -std=gnu++11 -O2 -o program.exe program.cpp
However, it is possible for users to override compile flags using pragma directives. For example, the optimisation level can be overridden with this code:
#pragma GCC optimize("Ofast")
We have no control over the source code which is submitted for compilation but we need to prevent compile options from being overridden.
Is there a way to ignore pragma directives with GCC?
Upvotes: 1
Views: 394
Reputation: 155604
From a quick scan of the man page, the answer is "No", gcc
has no "disable pragmas" feature.
As a truly ridiculous workaround, you might simple require all entries to omit the string pragma
(case-insensitive to block the _Pragma
operator as well) entirely, rejecting any that violate that requirement. Anything more complicated that that will essentially entail writing your own C source code parser, and I doubt it's worth the trouble. Not really sure even that will work, given the token concatenation tricks you can play with the preprocessor (I await a comment demonstrating such an exploit).
Upvotes: 2