Logical error in code - error-handling

Code that I am supposed to fill out which was easy enough.
#define MAX_NAME_LEN 128
typedef struct {
char name[MAX_NAME_LEN];
unsigned long sid;
} Student;
/* return the name of student s */
const char* getName(const Student* s) {
return s->name;
}
/* set the name of student s */
void setName(Student* s, const char* name) {
/* fill me in */
}/* return the SID of student s */
unsigned long getStudentID(const Student* s) {
/* fill me in */
}
/* set the SID of student s */
void setStudentID(Student* s, unsigned long sid) {
/* fill me in */
}
However it says what is the logical error in the following function?
Student* makeDefault(void) {
Student s;
setName(&s, "John");
setStudentID(&s, 12345678);
return &s;
}
I do not see any problems. I tested it. It works fine.
Is it because this should probably be a void function and does not need to be returning anything?

You can't return a pointer to a locally declared variable (Student s). The variable "s" will disappear (become garbage) after the return.
Instead, you need to allocate a student first.
You should probably do this:
void makeDefault(Student* pS) {
setName( pS, "John");
setStudentID( pS, 12345678);
}
And then let the calling application allocate the student.

Related

Arduino - passing values by reference from lamda to singleton

Hello i am bigginer in programing and i have specific problem.
I have been learning a new ways to write a code in small Arduino project.
that project have multiple objects like distance measuring Senzor, led diods , temperature senzor, etc. And all this objects have its own menu where you can, for example, start a calibration or just get values.
What i need is singleton class that has a function enter_esc() that need a int (*funct)() parameter basically function pointer.
That enter_esc(int (*funct)()) function just looping function until you press escape pin which is defined.
function Calibration() have inside some private: object data types like value or cali_value.
so i tried to insert function Calibration() right into enter_esc(Calibration) but it won't compile becouse i didnt pass that vlaues by reference or copy.
but what i found is lambda.
i made a lamda similar to a Calibration() function and i passed values by reference &{//domething;}
but i had to use enter_esc(std::function<int()>& funct) whitch is only int C++ standard library and not in Arduino C/C++ so my qestion is:
[is there some way how to pass values by reference by using lambda to a singleton class in Arduino ?]
(i konw it can be done differently but like i said i want to learn some new ways to program, also if you have some different way to make it i will by very happy to see it)
10Q for your time :)
//Class.h
#pragma once
class events {
private:
static events e_instance;
int p_menu, p_enter, p_esc, p_up, p_down;
int menuValue;
events();
public:
events(const events&) = delete;
static events& Get();
int ArrowUpDown(int maxVal);
int ArrowUpDown(int p_up, int p_down, int maxVal);
int enter_esc(const std::function<int()>& funct);
};
events events::e_instance;
class deviceBase : public Printables
{
public:
const char* a_pin;
int d_pin;
String type;
String deviceName;
bool inUse;
int actualCount;
public:
String getType() override;
int getActualCount() override;
String getName() override;
String getInUse() override;
};
class senzor : public deviceBase
{
private:
int Value;
int triggValue;
public:
int p_triggValue = 10;
static int allSenzors;
friend events;
senzor();
~senzor();
public:
int getValue();
int Calibration();
void changeTriggVal(int x);
void Reset();
void nullCalibration();
void Menu(int x);
void setName(String deviceName);
void setInUse(bool x);
int getPin();
};
int senzor::allSenzors = 0;
if you have some good advice to my code writing i will be also very glad
//Class.cpp
#include <iostream>
#include <string>
#include <functional>
#define LOG(x) std::cout << x << std::endl;
#define PINMENU 12
#define PINENTER 8
#define PINESC 9
#define PINUP 11
#define PINDOWN 13
using String = std::string;
struct Printables
{
virtual String getType() = 0;
virtual int getActualCount() = 0; ;
virtual String getName() = 0;
virtual String getInUse() = 0;
};
#include "Class.h"
events& events::Get() {
return e_instance;
}
int events::ArrowUpDown(int maxVal) {
if (maxVal) {
menuValue = menuValue < maxVal ? menuValue++ : menuValue;
}
if (maxVal) {
menuValue = menuValue > 0 ? menuValue-- : menuValue;
}
return menuValue;
}
int events::enter_esc(const std::function<int()>&funct) {
if (1) {
while (!p_esc) {
auto f = funct;
}
}
return 1;
}
int events::ArrowUpDown(int p_up, int p_down, int maxVal) { return 666; }
events::events() {};
String deviceBase::getType() { return type; }
int deviceBase::getActualCount() { return actualCount; }
String deviceBase::getName() { return deviceName; }
String deviceBase::getInUse() {
String Status;
Status = inUse == 1 ? "Active" : "Deactive";
return Status;
}
senzor::senzor() : Value(0), triggValue(1) {
a_pin = "xx";
type = "[SENZOR]";
deviceName = "[UNKNOWN]";
inUse = 0;
allSenzors++;
actualCount = allSenzors;
a_pin = 0;
}
senzor::~senzor() {
allSenzors = 0;
}
int senzor::getValue() {
Value = 4;
return Value;
}
int senzor::Calibration() {
triggValue = triggValue < getValue() ? getValue() : triggValue;
p_triggValue = triggValue;
return p_triggValue;
}
void senzor::changeTriggVal(int x) {
p_triggValue = x;
}
void senzor::Reset() {
p_triggValue = triggValue;
}
void senzor::nullCalibration() {
triggValue = 1;
}
void senzor::setName(String deviceName) {
this->deviceName = deviceName;
}
void senzor::setInUse(bool x) {
inUse = x;
}
int senzor::getPin() {
return 4;
}
int printsss() {
return 1;
}
////////////////////////////////this what i was writing about//////////////////////////////
void senzor::Menu(int x) {
events::Get().enter_esc([&]() { triggValue = triggValue < getValue() ? getValue() : triggValue;
p_triggValue = triggValue;
return p_triggValue; });
}
but if i use lambda in arduino with enter_esc(int (*funct)()) i get this kind of error
no matching function for call to 'events::enter_esc(senzor::Menu(int)::<lambda()>)'

EXPECT_CALL not able to track the calls for methods called from the method under test

First of all, I'm new to Google test, so please forgive my ignorance, if any.
I'm writing unit tests using google test framework for the below class..
class MsgHandler
{
public:
MsgHandler(){}
~MsgHandler(){}
bool decode_data(unsigned char*, unsigned int,char *);
bool handle_req_state_data(char *, char *);
};
bool MsgHandler::handle_req_state_data(char *a, char *b)
{
printf("handle_req_state_data called\n");
return true;
}
bool MsgHandler::decode_data(unsigned char *a, unsigned int b,char *c)
{
printf("decode_data called\n");
handle_req_state_data(c, c);
return true;
}
As a first step, I have created the mock class as below
class MsgHandlerMock : public MsgHandler
{
public:
MsgHandlerMock()
{
}
virtual ~MsgHandlerMock() {}
MOCK_METHOD(bool, handle_req_state_data, (char *, char *), (const));
//MOCK_METHOD(int, decode_data, (unsigned char*, unsigned int ,char* ));
private:
};
Below is the test function
TEST_F(TestClass, test01)
{
MsgHandlerMock mockObj;
//EXPECT_CALL(mockObj, decode_data(::testing::_,::testing::_,::testing::_)).Times(1);
EXPECT_CALL(mockObj, handle_req_state_data(::testing::_,::testing::_)).Times(1);
mockObj.decode_data(0,0,0);
}
My intention of this test is to make sure 'handle_req_state_data' is called when I call 'decode_data' with a certain message. But my test fails with below error
decode_data called
handle_req_state_data called
...
Actual function call count doesn't match EXPECT_CALL(mockObj, handle_req_state_data(::testing::_,::testing::_))...
Expected: to be called once
Actual: never called - unsatisfied and active
[ FAILED ] TestClass.test01 (1 ms)
[----------] 1 test from TestClass (1 ms total)
Can someone help me on how to validate the inner method calls with EXPECT_CALL validators?
The function is not virtual, so can't be overridden by the mock, decode_data does not use dynamic dispatch and call the original method MsgHandler::handle_req_state_data.
virtual bool handle_req_state_data(char *, char *);
MOCK_METHOD(bool, handle_req_state_data, (char *, char *), (override));

Function not working when inside if or switch statements

I need to solve a OOP problem in which I have to manage multiple classes inherited by each other. First I need to read all the data for all the Employees of a Company. The reading runs very well but I also need to display the read data after reading the command 1 (I need to use switch). I created a function "afisare_angajati()" which only works outside "if" and "switch" statements. I don't know why those statements disable my function. This happened to me before but I couldn't find the cause. Is something that I am not seeing? You can see my function at the end of the code. Thx for help.
#include<iostream>
#include<string>
#include<vector>
class Angajat{
protected:
std::string nume;
float salariu_baza;
std::string functie;
float procent_taxe_salariu;
public:
float get_salariu_net(){return 0;}
float get_salariu_brut(){return 0;}
std::string get_nume(){return 0;}
void marire_salariu(){}
Angajat(std::string nume,float salariu_baza,std::string functie,float procent_taxe_salariu=40):
nume(nume),salariu_baza(salariu_baza),functie(functie),procent_taxe_salariu(procent_taxe_salariu){}
void display(){
std::cout<<nume<<'\n';
std::cout<<functie<<'\n';
}
};
class Analist:public Angajat{
public:
Analist(std::string nume,float salariu_baza,std::string functie,float procent_taxe_salariu=40):
Angajat(nume,salariu_baza,functie,procent_taxe_salariu){}
};
class Programator:public Analist{
protected:
float procent_deducere_salariu_it;
public:
Programator(std::string nume,float salariu_baza,std::string functie,float procent_taxe_salariu=40):
Analist(nume,salariu_baza,functie,procent_taxe_salariu){}
};
class LiderEchipaProgramare:public Programator{
protected:
int vechime;
float bonus_vechime;
public:
LiderEchipaProgramare(std::string nume,float salariu_baza,std::string functie,int vechime,float procent_taxe_salariu=40):
Programator(nume,salariu_baza,functie,procent_taxe_salariu),vechime(vechime){
bonus_vechime=500;
}
};
class FirmaProgramare{
private:
std::vector<Angajat*> vec_ang;
public:
void afisare_angajati(){
for(Angajat* a:vec_ang){
a->display();
}
}
void mareste_salarii(float,float,float){}
void promoveaza(std::string){}
void adauga_angajat(Angajat* a){
vec_ang.push_back(a);
}
};
int main(){
std::string nume;
std::string functie;
float salariu_baza;
int vechime;
int nr_ang,comanda;
FirmaProgramare pula;
std::cin>>nr_ang;
for(int i=0;i<nr_ang;++i){
std::cin.ignore();
std::getline(std::cin,nume);
std::cin>>functie;
std::cin>>salariu_baza;
Angajat* p = nullptr;
if(functie=="Analist"){
p = new Analist(nume,salariu_baza,functie);
}
else{
if(functie=="Programator"){
p = new Programator(nume,salariu_baza,functie);
}
else{
p = new LiderEchipaProgramare(nume,salariu_baza,functie,vechime);
}
}
pula.adauga_angajat(p);
}
std::cin>>comanda;
//pula.afisare_angajati(); output is correct if I put the function outside of brackets
switch(comanda)
{
case 1:{
pula.afisare_angajati();
break;
}
}
}

how to print all the vxWorks wvEvents

Is there a vxWorks command that lists all the wv events that are compiled in the code. I am trying to segregate all the events on one of our products which uses vxWorks 6.5.
No there isn't a command giving you a list of WindView events of your code.
Why don't you search for all lines containing wvEvent of all your source files and remove the duplicates? This should produce a list of all events...
--- Update ---
I've written the following small library to hook symbols within the VxWorks symbol table. This can be used to hook system calls within applications loaded after a symbol was hooked.
The following mechanism works since the ld command resolves all unresolved references of the application using the system symbol table (e.g. calls to printf are replaced with the value of the symbol printf which is the address of the printf implementation).
Here is the code of the library:
#include <vxWorks.h>
#include <symLib.h>
#define MAX_HOOKS 256
typedef struct vxhook_struct
{
SYMTAB_ID symTblId; /** id of symbol table containing the symbol */
SYMBOL *symbol; /** pointer to symbol entry within symbol table */
void *originalValue; /** original value of symbol */
void *hookedValue; /** hooked value of symbol */
} VXHOOK;
/** counter of installed hooks */
static int hooks = 0;
/** list of installed hooks */
static VXHOOK hook[MAX_HOOKS];
/*
** symbolIterator
** VxWorks callback for symbol table iteration. See symEach(..) for more information.
*/
static BOOL symbolIterator(char *name,
int val,
SYM_TYPE type,
int arg,
UINT16 group)
{
BOOL result = TRUE; /* continue iteration */
char *pName;
pName = (char *)arg;
if ((pName != NULL) && (name != NULL))
{
if (!strcmp(name, pName))
{
result = FALSE; /* symbol found => stop iteration! */
}
}
return (result);
}
/*
** vxHookGet
** Searches the hook list for a specific entry and returns a pointer to it if found.
*/
static VXHOOK *vxHookGet(SYMTAB_ID symTblId, /* symbol table to seachr for name */
char *name, /* name of symbol */
int *index) /* optional return value of index within hook list */
{
VXHOOK *pHook = NULL;
int i;
if (index != NULL)
{
*index = -1;
}
for (i = 0; i < hooks; i++)
{
if ((hook[i].symTblId == symTblId) && (!strcmp(hook[i].symbol->name, name)))
{
pHook = &(hook[i]);
if (index != NULL)
{
*index = i;
}
break;
}
}
return (pHook);
}
/*
** vxHook
** Hooks a symbol within a symbol table (if not already hooked).
*/
STATUS vxHook(SYMTAB_ID symTblId, /* symbol table to seachr for name */
char *name, /* name of symbol */
void *value, /* new value to replace symbol value with */
void **pValue) /* optional pointer receiving original value from symbol table */
{
STATUS result = ERROR;
SYMBOL *symbol;
VXHOOK *pHook;
if ((name != NULL) && (value != NULL))
{
pHook = vxHookGet(symTblId, name, NULL);
if (pHook == NULL) /* symbol not hooked, yet? */
{
if (hooks < MAX_HOOKS) /* free entries available? */
{
pHook = &(hook[hooks]);
symbol = symEach(symTblId, symbolIterator, (int)name);
if (symbol != NULL) /* symbol found? */
{
pHook->symTblId = symTblId;
pHook->symbol = symbol;
pHook->originalValue = symbol->value;
pHook->hookedValue = value;
if (pValue != NULL)
{
*pValue = symbol->value;
}
/* install hook */
symbol->value = value;
printf("Value of symbol '%s' modified from 0x%x to 0x%x.\n", name, pHook->originalValue, pHook->hookedValue);
hooks++;
result = OK;
}
else
{
printf("Symbol '%s' not found in symTblId=0x%08x.\n", name, symTblId);
}
}
else
{
printf("Maximum number of hooks (%d) reached.\n", MAX_HOOKS);
}
}
else
{
printf("Symbol '%s' in symTblId=0x%08x already hooked.\n", name, symTblId);
}
}
return (result);
}
/*
** vxHookList
** Prints a list of all installed hooks to stdout.
*/
STATUS vxHookList(void)
{
int i;
printf(" name symTblId value original\n");
printf("------------------- ---------- ---------- ----------\n");
for (i = 0; i < hooks; i++)
{
printf("%3d: 0x%08x %s\n", i, hook[i].symTblId, hook[i].symbol->name);
}
return (OK);
}
/*
** vxUnhook
** Unhooks a hooked symbol (restoring the original value).
*/
STATUS vxUnhook(SYMTAB_ID symTblId, /* symbol table to search for name */
char *name) /* name of symbol */
{
STATUS result = ERROR;
VXHOOK *pHook;
int i;
pHook = vxHookGet(symTblId, name, &i);
if (pHook != NULL)
{
pHook->symbol->value = pHook->originalValue;
printf("Original value 0x%x of symbol '%s' restored.\n", pHook->originalValue, name);
/* remove hook */
hooks--;
/* shift remaining hooks... */
for (; i < hooks; i++)
{
memcpy(&(hook[i]), &(hook[i + 1]), sizeof(VXHOOK));
}
result = OK;
}
else
{
printf("Hook for '%s' (symTblId=0x%08x) not found.\n", name, symTblId);
}
return (result);
}
To hook wvEvent(..) calls you have to do the following:
#include <wvLib.h>
#include <symLib.h>
STATUS (*WVEVENT_FPTR)(event_t, char *, size_t);
WVEVENT_FPTR orig_wvEvent = wvEvent;
STATUS my_wvEvent(event_t usrEventId, /* event */
char * buffer, /* buffer */
size_t bufSize) /* buffer size */
{
/* create a list of usrEventId... */
return (orig_wvEvent(usrEventId, buffer, bufSize));
}
STATUS hookWvEvents(void)
{
STATUS result = ERROR;
result = vxHook(sysSymTbl, "wvEvent", my_wvEvent, &orig_wvEvent);
return (result);
}
After calling hookWvEvents you can load your application and all calls to wvEvent from within your application will be redirected to my_wvEvent (and then passed on to the original wvEvent function). Remember that hooked symbols stay hooked for your application even if you unhook the symbol using vxUnhook.
Note: This mechanism is also very helpful for application testing and debugging since you can stress your application by forcing system calls to fail...

Is there an equivalent to __attribute__((ns_returns_retained)) for a malloc'd pointer?

I'm looking for an annotation something like
-(SomeStruct *) structFromInternals __attribute__((returns_malloced_ptr))
{
SomeStruct *ret = malloc(sizeof(SomeStruct));
//do stuff
return ret;
}
to soothe the clang static analyzer beasts.
The only viable attributes link I can find is for GCC, but it doesn't even include ns_returns_retained, which is in an extension, I assume.
EDIT:
as to why this is needed, I have a scenario that I can't repro in a simple case, so it may have to do with a c lib in an Objective-C project... The gist is, I get a static analyzer warning that the malloc in createStruct is leaked:
typedef struct{
void * data;
size_t len;
}MyStruct;
void destroyStruct(MyStruct * s)
{
if (s && s->data) {
free(s->data);
}
if (s) {
free(s);
}
}
MyStruct * createStructNoCopy(size_t len, void * data)
{
MyStruct * retStruct = malloc(sizeof(MyStruct));
retStruct->len = len;
retStruct->data = data;
return retStruct;
}
MyStruct * createStruct(size_t len, void * data)
{
char * tmpData = malloc(len);
memcpy(tmpData, data, len);
return createStructNoCopy(len, tmpData);
}
MyStruct * copyStruct(MyStruct * s)
{
return createStruct(s->len, s->data);
}
The function annotation ownership_returns(malloc) will tell the Clang static analyser that the function returns a pointer that should be passed to free() at some point (or a function with ownership_takes(malloc, ...)). For example:
void __attribute((ownership_returns(malloc))) *my_malloc(size_t);
void __attribute((ownership_takes(malloc, 1))) my_free(void *);
...
void af1() {
int *p = my_malloc(1);
return; // expected-warning{{Potential leak of memory pointed to by}}
}
void af2() {
int *p = my_malloc(1);
my_free(p);
return; // no-warning
}
(See the malloc-annotations.c test file for some more examples of their use.)
At the moment, these annotations only take effect when the alpha.unix.MallocWithAnnotations checker is run (which is not run by default). If you're using Xcode, you'll need to add -Xclang -analyzer-checker=alpha.unix.MallocWithAnnotations to your build flags.