C++, VS2005 Please help me to resolve this compilation error - visual-c++

Code
BOOL CGrAllObjects::ReorderObj(CGrObject* pGrFind,int ixObjNewIx)
{
int nSubCode,nLyrCode=pGrFind->GetLayerCode(nSubCode);
CGrObject* pGrObject;
CGrObjectArray*
pGrObjectArray=GetObjArrayFromCode(nLyrCode,nSubCode);
if(!pGrObjectArray) return FALSE;
for(int ixObj=pGrObjectArray->GetSize()-1; ixObj>=0; ixObj--)
{ pGrObject=pGrObjectArray->GetAt(ixObj);
if(pGrObject==pGrFind) break;
}
if(ixObj<0) return FALSE;
if(ixObj!=ixObjNewIx)
{ pGrObjectArray->RemoveAt(ixObj);
pGrObjectArray->InsertAt(ixObjNewIx,pGrFind);
}
return TRUE;
}
Error: 1>c:\xxx\xxx\xxxx\xxxx\xxxx\xxxxx\xxxx.cpp(359) : error C2065:
'ixObj' : undeclared identifier

for(int ixObj ... the variable "ixObj" is only defined in the scope of the for loop and is not known outside.
Define the integer before the loop, and remove the decalration from 'for':
int ixObj;
for(ixObj=...

Related

DART: Passing function in a function as parameter

The below snippet of code gives me error in dartpad. But the same code works fine in the course which I am doing online. I am using dart version 2.8.4.
void main() {
int var = operation(5,5,add); --> Error
//print(operation(5,5,add));
}
// class calculator {
// calculator({this.operand});
// }
int add(int n1, int n2) {
return n1+n2;
}
int multiply(int n1, int n2) {
return n1*n2;
}
int operation(int n1, int n2, Function operand){
return operand(n1, n2);
}
Error:
Error compiling to JavaScript:
main.dart:3:2:
Error: Expected ';' after this.
int var = operation(5,5,add);
^^^
main.dart:3:10:
Error: Expected an identifier, but got '='.
int var = operation(5,5,add);
^
Error: Compilation failed.
int var = operation(5,5,add); don't use var as a variable name, it a keyword used in the dartlang, just change the name to anything else and you wont get any error:
int operationResult = operation(5,5,add);

How can I match a pointer to a null object?

I'd like to match on all the ways a particular argument to a function can be null. Right now I'm using
hasArgument(
3,
anyOf(
cxxNullPtrLiteralExpr()
,integerLiteral() // Technically this would alert on a constant pointer; but that's madness
)
)
However this doesn't match the following code:
void* nullObj = nullptr;
function(nullptr, false, false, nullObj);
Is it possible/easy to track this and match it? Right now I have a very simpler matcher but I guess this type of analysis requires considerably more logic?
High-level answer
You can't just "match" an expression whose value is NULL. AST matching can only inspect the syntax of the argument, so if the argument is not a literal, you don't know if it might be NULL.
Instead, you need to use a flow-sensitive checker that queries the Clang SA constraint engine. The constraint engine tracks values as they flow through the program.
The core of such a checker looks like this:
bool NullArgChecker::evalCall(const CallExpr *CE, CheckerContext &C) const {
ProgramStateRef state = C.getState();
auto SVal = C.getSVal(CE->getArg(0)).getAs<DefinedOrUnknownSVal>();
if (SVal) {
ConditionTruthVal Nullness = state->isNull(*SVal);
if (Nullness.isConstrainedTrue()) {
Given a call expression CE, we get its first argument, then query the CheckerContext for the symbolic value SVal associated with the first argument. We then ask if that value is known to be NULL.
Complete example
Here is a complete example checker that reports a warning every time it sees a value known to be NULL being passed as the first argument of any function.
NullArgChecker.cpp:
// NullArgChecker.cpp
// https://stackoverflow.com/questions/57665383/how-can-i-match-a-pointer-to-a-null-object
#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
#include "clang/StaticAnalyzer/Core/Checker.h"
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
using namespace clang;
using namespace ento;
namespace {
class NullArgChecker : public Checker< eval::Call > {
mutable std::unique_ptr<BuiltinBug> BT_nullarg;
public:
NullArgChecker() {}
bool evalCall(const CallExpr *CE, CheckerContext &C) const;
};
} // end anonymous namespace
bool NullArgChecker::evalCall(const CallExpr *CE, CheckerContext &C) const {
ProgramStateRef state = C.getState();
auto SVal = C.getSVal(CE->getArg(0)).getAs<DefinedOrUnknownSVal>();
if (SVal) {
// This is the core of this example checker: we query the constraint
// engine to see if the symbolic value associated with the first
// argument is known to be NULL along the current path.
ConditionTruthVal Nullness = state->isNull(*SVal);
if (Nullness.isConstrainedTrue()) {
// Create a warning for this condition.
ExplodedNode *N = C.generateErrorNode();
if (N) {
if (!BT_nullarg) {
BT_nullarg.reset(new BuiltinBug(
this, "Null Argument", "The first argument is NULL."));
}
C.emitReport(llvm::make_unique<BugReport>(
*BT_nullarg, BT_nullarg->getDescription(), N));
}
}
}
return false;
}
void ento::registerNullArgChecker(CheckerManager &mgr) {
mgr.registerChecker<NullArgChecker>();
}
bool ento::shouldRegisterNullArgChecker(const LangOptions &LO) {
return true;
}
Changes to other files to hook this in:
--- a/clang/include/clang/StaticAnalyzer/Checkers/Checkers.td
+++ b/clang/include/clang/StaticAnalyzer/Checkers/Checkers.td
## -148,6 +148,10 ## def NonnullGlobalConstantsChecker: Checker<"NonnilStringCon
stants">,
let ParentPackage = CoreAlpha in {
+def NullArgChecker : Checker<"NullArg">,
+ HelpText<"Check for passing a NULL argument">,
+ Documentation<NotDocumented>;
+
def BoolAssignmentChecker : Checker<"BoolAssignment">,
HelpText<"Warn about assigning non-{0,1} values to Boolean variables">,
Documentation<HasAlphaDocumentation>;
--- a/clang/lib/StaticAnalyzer/Checkers/CMakeLists.txt
+++ b/clang/lib/StaticAnalyzer/Checkers/CMakeLists.txt
## -62,6 +62,7 ## add_clang_library(clangStaticAnalyzerCheckers
NonNullParamChecker.cpp
NonnullGlobalConstantsChecker.cpp
NullabilityChecker.cpp
+ NullArgChecker.cpp
NumberObjectConversionChecker.cpp
ObjCAtSyncChecker.cpp
ObjCAutoreleaseWriteChecker.cpp
Example input to test it on:
// nullargpp.cpp
// Testing NullArg checker with C++.
#include <stddef.h> // NULL
void somefunc(int*);
void nullarg1()
{
somefunc(NULL); // reported
somefunc(0); // reported
somefunc(nullptr); // reported
}
void nullarg2()
{
int *p = 0;
somefunc(p); // reported
}
void nullarg3(int *p)
{
if (p) {
somefunc(p); // not reported
}
else {
somefunc(p); // reported
}
}
void not_nullarg(int *p)
{
somefunc(p); // not reported
}
Example run:
$ g++ -std=c++11 -E -o nullargpp.ii nullargpp.cpp
$ ~/bld/llvm-project/build/bin/clang -cc1 -analyze -analyzer-checker=alpha.core.NullArg nullargpp.ii
nullargpp.cpp:10:3: warning: The first argument is NULL
somefunc(
^~~~~~~~~
nullargpp.cpp:11:3: warning: The first argument is NULL
somefunc(0);
^~~~~~~~~~~
nullargpp.cpp:12:3: warning: The first argument is NULL
somefunc(nullptr);
^~~~~~~~~~~~~~~~~
nullargpp.cpp:18:3: warning: The first argument is NULL
somefunc(p);
^~~~~~~~~~~
nullargpp.cpp:27:5: warning: The first argument is NULL
somefunc(p);
^~~~~~~~~~~
5 warnings generated.
For maximum specificity, the above changes were made to llvm-project commit 05efe0fdc4 (March 2019), running on Linux, but should work with any Clang v9.

cannot set 'name' of undefined when throwing exception

I was just trying Apache Thrift in nodejs before using it in my upcoming project wherein I ran into this error.
Here is my demo.thrift file
namespace js demo
typedef i32 int
enum Operation {
ADD = 1,
SUBTRACT = 2,
MULTIPLY = 3,
DIVIDE = 4
}
struct Work {
1: int num1 = 0,
2: int num2,
3: Operation op,
4: optional string comment
}
exception InvalidOperation {
1: int message,
2: string trace
}
service Calculator {
void ping()
double calculate(1: int logid, 2: Work w) throws (1: InvalidOperation oops),
oneway void zip()
}
Here is a part of the server.js
I use switch case to determine operation in server.js
// inside thrift.createServer
calculate: (logid, work, result) => {
let answer = null, oops = null;
switch(work.op) {
// Code related to Operation.ADD, Operation.SUBTRACT ...
default: {
console.log("ERROR!");
oops = InvalidOperation();
oops.message = work.op;
oops.trace = "Unknown Operation";
}
}
result(oops, answer);
}
When the client.js calls server with calculate(12345, { num1:1, num2:2, op: 10 })
Instead of returning an error it throws
TypeError: Cannot set property 'name' of undefined in demo_types.js:122
The part related to InvalidOperation in demo_types.js is
// Work related code
var InvalidOperation = module.exports.InvalidOperation = function(args) {
Thrift.TException.call(this, "InvalidOperation");
this.name = "InvalidOperation"; // points to here
this.message = null;
this.trace = null;
if (args) {
if (args.message !== undefined && args.message !== null) {
this.message = args.message;
}
if (args.trace !== undefined && args.trace !== null) {
this.trace = args.trace;
}
}
};
Thrift.inherits(InvalidOperation, Thrift.TException);
InvalidOperation.prototype.name = 'InvalidOperation';
// InvalidOperation.read & .write
Any idea why the error is being thrown?
Actually I realised why this error is being thrown. It is a plain old Javascript mistake.
oops = new InvalidOperation();
That's it.

g++ error: expected ; before "it"

I'm porting C++ app from Solaris to Linux and I'm stuck with the following error. The code is:
template <class MapSuperClass> class FWPointerMap : public MapSuperClass
{
public:
FWPointerMap()
{
_wipe = false;
}
FWPointerMap(const MapSuperClass* mMap)
{
MapSuperClass::const_iterator it = mMap->begin(); // line 50
while(it != mMap->end())
{
insert(MapSuperClass::value_type((*it).first, (*it).second));
it++;
}
_wipe = false;
}
And I get the following error:
../../framework/fwcore/hdr/FWPointerMap: In constructor FWPointerMap<MapSuperClass>::FWPointerMap(const MapSuperClass*):
../../framework/fwcore/hdr/FWPointerMap:50: error: expected ; before it
../../framework/fwcore/hdr/FWPointerMap:52: error: it was not declared in this scope
I think you just need to add 'typename' to tell the compiler that MapSuperClass::const_iterator is a type:
typename MapSuperClass::const_iterator it = mMap->begin(); // line 50
Because MaySuperClass is a class template parameter, the assumption is that the const_iterator member is a field. Using typename informs the compiler that it is in fact a type.
More information: http://en.wikipedia.org/wiki/Typename#A_method_for_indicating_that_a_dependent_name_is_a_type

Visual C++/CLI (CLR) Null pointer

I wan't to implement the following code - the check if the pointer is null or not null. If the pointer points to object, then do sth with that object, if not - skip that code block.
My code:
ref class EchoClient {
private:
GameMatrix^ gameMatrix;
public:
EchoClient(void);
EchoClient(GameMatrix^);
void do();
};
EchoClient::EchoClient(void)
{
this->gameMatrix = NULL;
}
EchoClient::EchoClient(gameMatrix)
{
this->gameMatrix = gameMatrix;
}
void EchoClient::do() {
if(this->gameMatrix != NULL)
{
this->gameMatrix->redrawMatrix();
}
}
The error:
error C2446: '!=' : no conversion from 'int' to 'GameMatrix ^' k:\visual studio 2010\Projects\EchoClient3WS\EchoClient3WS\EchoClient.cpp 106
Any solutions ???
nullptr

Resources