int auth(){stuff}; auth(); ERROR: expected initializer brefore int - c++

EDIT: I started from scratch, going with a user+pass authentication for better security.
I posted a new question here.
I'm writing a program that checks that the computer running it has a specific hostname (as an authentication method).
#include <iostream>
using namespace std;
void RUN()
{
//Do stuff
}
char const* HOSTNAME
int auth()
{
if (HOSTNAME = getenv("HOSTNAME"))
{
HOSTNAME = getenv("HOSTNAME");
return 0;
}
else if(HOSTNAME = getenv("COMPUTERNAME"))
{
HOSTNAME = getenv("COMPUTERNAME");
return 0;
}
else
{
return 1;
};
}
int main()
{
cout << "Authenticating..." << endl; auth();
if (HOSTNAME == "Craptop")
{
cout << "Success!" << endl;
RUN();
}
else
{
cout <<
cout << "Authentication failed! Exiting..." << endl;
exit(0);
};
return 0;
}
Im getting this error: (should be the initializer but seams to act like a variable)
./EX_host_locked.cpp:5:1: error: expected initializer before ‘int’
int auth()
^~~
since int acts like a variable, auth is uninitialized and causing more errors later.
How can I make int be the initializer for auth? Also how could I generally improve my code (for security/stability)?

You are missing ; after line following line:
char const* HOSTNAME
So you are getting the error. Put ; and make it:
char const* HOSTNAME;

Related

Program with a separate thread is being killed

After declaring an object in my main and running its function in a separate thread, my program crashes.
I have read other question on SO but due to my lack of knowledge in multithreading, I cannot understand what is my specific problem.
Here is my class called UART (without header files and only showing the required cpp declaration):
void UART::run()
{
while(true)
{
_letter = _serial.read();
if (_letter == "!")
{
_line = _serial.readline();
_words.clear();
std::istringstream f(_line);
std::string s;
//std::cout << _letter << std::endl;
while (getline(f,s,'\t'))
{
_words.push_back(s);
}
this->fillVars();
}
}
}
void UART::fillVars()
{
if (_words[0] == "s")
{
_effort[0] = std::stoi(_words[1]);
_effort[1] = std::stoi(_words[2]);
}
else if (_words[0] == "e")
{
this->convertToMeters();
}
}
void UART::convertToMeters()
{
std::cout << _position[0];
_position[0] = std::stod(_words[1]); // / _tick_meters;
_position[1] = std::stod(_words[2]) / _tick_meters;
}
double UART::getPosition(std::string wheel)
{
if (wheel == "LEFT") return _position[0];
else return _position[1];
}
And my main cpp looks like this:
int main(int argc, char** argv)
{
ros::init(argc, argv, "joint_node");
std::string port("/dev/ttyACM0");
unsigned long baud = 115200;
try
{
serial::Serial my_serial(port, baud, serial::Timeout::simpleTimeout(1000));
if(my_serial.isOpen()) ROS_INFO("Serial is %s", "open");
genius::UART uart(my_serial, 380);
std::thread uart_run(&genius::UART::run, uart);
std::cout << uart.getPosition("LEFT") <<std::endl;
} catch (std::exception &e)
{
std::cerr << "Unhandled Exception: " << e.what() << std::endl;
}
return 0;
}
My understanding is that after creating an object uart, I want to run its function run() in a separate thread as I want its values to be updated with no interruption on a background. So whenever I access its values like using its function uart.getPosition("LEFT") I will get the last up-to date data. I guess I do not need .join() this thread as I do not want to wait for it as it never ends.
But for some reason after calling the uart.getPosition("LEFT") my program crashes and also function getPosition() never gets executed and I always get value of 0.

Memory access violation error c++

I'm trying to exchange messages using multiple covert channels.
So, basically, first i need to select the channel that i want to use for communication and then select the "destination_ip" of the user that i want to chat with and after that the
processMessage()
is called. Now, to move from one channel to another I have to close the existing connection and then open a new connection with the new channel that i want to use. My code below is modified to keep using the same channel after closing the connection and contain only the things that you need.
#include <channelmanager.hpp>
#include <thread>
#include <iostream>
#include <boost/test/unit_test.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/lexical_cast.hpp>
#include <stdio.h>
#include <string.h>
#include <fstream>
#include <openssl/hmac.h>
struct CommunicationFixture {
CommunicationFixture() {
channelmanager.setErrorStream(&cout);
channelmanager.setOutputStream(&cout);
destination_ip = "";
channel_id = channelmanager.getChannelIDs()[0];
}
library::ChannelManager channelmanager;
vector<string> last_adapters;
string destination_ip;
string channel_id = "";
int processMessage(string message) {
if (message.compare("exit") == 0) {
channelmanager.closeConnection(destination_ip);
return 1;
}
vector<string> arguments;
boost::split(arguments, message, boost::is_any_of(" "), boost::token_compress_on);
if (arguments[0].compare("argument") == 0) {
if (arguments.size() < 2) {
cout << "Not enough arguments" << endl;
return 0;
}
string argument_list = arguments[1];
for (unsigned int i = 2; i < arguments.size(); i++) {
argument_list += " " + arguments[i];
}
channelmanager.setChannelArguments(destination_ip, argument_list);
cout << "Set channel argument to '" << argument_list << "'." << endl;
return 0;
}
if (message.compare("help") == 0) {
cout << "Help not available in chat mode. Close chat first with 'exit'" << endl;
return 0;
}
channelmanager.openConnection(destination_ip, channel_id);
channelmanager.sendMessage(destination_ip, message);
return 0;
}
int close(string destination){
cout << "closing.." << endl;
channelmanager.closeConnection(destination); //I believe i have the error because of this!
return 0;
}
};
BOOST_FIXTURE_TEST_SUITE(communication, CommunicationFixture)
BOOST_AUTO_TEST_CASE(basic_communication) {
selectAdapterId(0);
cout << "Test" << endl << endl;
printCommands();
cout << "Enter your command:" << endl;
string command;
int code = 0;
while (code != 2) {
std::getline(cin, command);
code = processCommand(command);
if (code == 1) {
// chat
cout << "chat started.." << endl;
int chatCode = 0;
while (chatCode != 1) {
std::getline(cin, message);
close(destination_ip);
chatCode = processMessage(message);
channelmanager.setErrorStream(&cout);
}
cout << "chat ended." << endl;
}
}
}
BOOST_AUTO_TEST_SUITE_END()
Note that, i think that the error happens due to the
function close()
because without it i don't get any errors. and the error doesn't happen immediately but after exchanging some messages. Here's the error:
unknown location(0): fatal error: in
"communication/basic_communication": memory access violation at
address: 0x00000024: no mapping at fault address
communicationTest.cpp(325): last checkpoint: "basic_communication"
test entry
Memory access violation happen when you are trying to access to an unitialized variable, in this case the channelmanager.
I can only see that you initialize channelmanager in the processMessage() method and you are closing the connection before initializing the channelmanager as it happen in:
close(destination_ip);
chatCode = processMessage(message);
Either you change the initialization or do not close it before the processMessage() method.
Memory access violation is also called a segmentation fault (or segfault), occurs when the program tries to access a memory location that doesn't exist, or is otherwise inaccessible. We call this trying to access an illegal memory location. That memory is either non-existent or we aren't aren't allowed to touch it.
If the first input from user is 'exit', which is going to call
if (message.compare("exit") == 0) {
channelmanager.closeConnection(destination_ip);
return 1;
}
In this case, destination_ip isn't initialised.

Segmentation fault while closing mysql connection in C++. How to resolve?

I am trying to query a database using the libmysql++ API and so far I am able to successfully run some queries. (reason behind some will be explained later)
So everything works fine, but as soon as I attempt at closing the connection and exiting the program, a Segmentation fault (core dumped) appears, as you can see below:
shell> g++ -o test testCDB.cpp -L/usr/include/mysql -lmysqlclient -I/usr/include/mysql
testCDB.cpp: In function ‘int main(int, char**)’:
testCDB.cpp:122:74: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
StudentManager manager1("localhost", "root", "ikilledkennedy.", "testDB");
^
testCDB.cpp:122:74: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
testCDB.cpp:122:74: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
testCDB.cpp:122:74: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
shell> ./test
Successfully connected to the database.
Enter your query: SHOW DATABASES;
[information_schema]
[mysql]
[performance_schema]
[sys]
[testDB]
Do you wish to continue?(Y:1/N:0) : 1
Enter your query: SHOW TABLES;
[Student]
Do you wish to continue?(Y:1/N:0) : 0
Segmentation fault (core dumped)
shell>
What's causing this issue and what measures do I take to resolve this?
Coming to the some queries part--there are certain queries that exit with a Segmentation fault too when fed to the database (like INSERT) which I feel is because these queries don't produce a result set and when I try to fetch rows in displayTable() or perhaps free the result set in performQuery(char *), a NULL value is encountered. Am I right in thinking this or is there some other root cause?
The code is as follows:
#include <iostream>
#include <string>
#include <mysql.h>
#include <limits>
using namespace std;
class StudentManager
{
private:
char *host;
char *user;
char *password;
char *database;
MYSQL *connection;
MYSQL_RES *result;
void init()
{
connection = mysql_init(NULL);
if(!mysql_real_connect(connection, host, user, password, database, 0, NULL, 0))
{
cout << "Cannot connect to the database.\n"
<< "Please make sure you have entered the details correctly and try again."
<< endl;
}
else
{
cout << "Successfully connected to the database.\n"
<< endl;
}
}
void displayTable()
{
MYSQL_ROW row;
unsigned int num_fields;
unsigned int i;
num_fields = mysql_num_fields(result);
cout << endl;
while((row = mysql_fetch_row(result)))
{
for(i = 0; i < num_fields; i++)
{
cout << "[" << ( row[i] ? row[i] : "NULL" ) << "]";
}
cout << endl;
}
cout << endl;
}
public:
StudentManager(char *host, char *user, char *password, char *database)
{
this->host = host;
this->user = user;
this->password = password;
this->database = database;
init();
}
void performQuery(char *query)
{
if(mysql_query(connection, query))
{
cout << "Query error: "
<< mysql_error(connection)
<< endl;
return;
}
result = mysql_use_result(connection);
displayTable();
mysql_free_result(result);
}
void disconnect()
{
cout << "Closing connection...";
mysql_close(connection);
cout << "Connection successfully closed.";
}
};
int main(int argc, char *argv[])
{
StudentManager manager1("localhost", "foo", "bar", "testDB");
int choice = 1;
char *query;
while(1)
{
cout << "Enter your query: ";
cin.getline(query, 256);
manager1.performQuery(query);
cout << "Do you wish to continue?(Y:1/N:0) : ";
cin >> choice;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
if(choice < 1)
{
manager1.disconnect();
break;
}
}
return 0;
}
You use uninitialized pointer here:
char *query;
while(1)
{
cout << "Enter your query: ";
cin.getline(query, 256);
The query variable is not initialized, and points to some random memory location. Most likely somewhere in the stack segment. When you write to that location using getline you corrupt the stack. Which leads to segmentation fault.
Since the code in question does not seem to be performance critical I'd suggest to rewrite the code like this:
std::string line;
while (cin.good())
{
cout << "Enter your query: ";
std::getline(cin, line);
PS there are some other issues with the code in my opinion, but since the question is about segmentation fault I'll keep my answer focused.
Looks like memory corrupted early, here:
char *query; // <-- uninitialized buffer
while(1)
{
cout << "Enter your query: ";
cin.getline(query, 256);
getline reads data from the stream and stores it into the buffer. Buffer of proper length should be allocated by you. To fix it use:
char query[256];
there are certain queries that exit with a Segmentation fault too when fed to the database (like INSERT) which I feel is because these queries don't produce a result set
INSERT query doesn't produce result set, so according to the documentation mysql_fetch_row(result) returns NULL. It's the correct behavior and not a reason of Segmentation fault.

Fingerprint calls cause segfaults in c++

I'm trying to write a simple fingerprint scanning program in c++ using libfprint, however it intermittently segfaults when run. Valgrind says the error is in the call to fp_enroll_finger which is consistent with my debugging, however beyond that I have absolutely no idea what is causing this error. Some of the times the program is run its fine, but some times it seems to consistently segfault for a period of time whenever the program is run?
Heres the code:
#include <iostream>
extern "C"
{
#include <libfprint/fprint.h>
}
using namespace std;
fp_dev * fpdev;
fp_print_data ** fpdata;
bool createDevice();
int main(int argc, char **argv)
{
int r = fp_init();
if(r != 0)
{
return r;
}
while(createDevice())
{
cout << "Scan right index finger" << endl;
int enrollStatus = fp_enroll_finger(fpdev, fpdata);
if(enrollStatus != 1)
{
cout << "Bad scan" << endl;
fp_dev_close(fpdev);
}
else
{
cout << "Good scan" << endl;
fp_print_data_save(fpdata[0], RIGHT_INDEX);
break;
}
}
if(fpdev != NULL)
{
fp_dev_close(fpdev);
}
fp_exit();
return 0;
}
bool createDevice()
{
fp_dscv_dev ** listOfDiscoveredDevs;
fp_dscv_dev * discoveredDevice;
listOfDiscoveredDevs = fp_discover_devs();
discoveredDevice = listOfDiscoveredDevs[0];
if(discoveredDevice != NULL)
{
cout << "Device found" << endl;
fpdev = fp_dev_open(discoveredDevice);
}
else
{
cout << "No device found" << endl;
return false;
}
fp_dscv_devs_free(listOfDiscoveredDevs);
return true;
}
You need to define fpdev and fpdata as:
fp_dev * fpdev;
fp_print_data * fpdata;
And use them as:
fp_enroll_finger(&fpdev, &fpdata);
Also don't forget to free fpdata when you no longer need it with fp_print_data_free
fp_dev * fpdev;
fp_print_data ** fpdata;
Will create 2 uninitialised pointers pointing to random memory location and leading to segfault once fp_enroll_finger will attempt to acces that location.
Checking fp_enroll_finger return value can be useful as well.

write/read struct to file using functions , Visual C++ 2008 express

I've made three files using Visual C++ 2008 express for a text based RPG game. Before I really dive into the whole thing I want to get the basics ironed out: new game, save game, continue game, quit game. So far I have the make a character section (in this case find a weapon) and quit game all ironed out. I'm stuck on how to pass the weapon stats from a struct to a save file. I appear to pass the members of the struct without issue and check the file to find "junk": Ì and -858993460 in place of my values.
How should I go about fixing my save_game and continue_game functions? I've done a lot of research trying to figure this out and nothing I've tried seems to help.
Here's the important pieces of the code:
struct player_character
{
char type;
int damage;
int stability;
};
void save_game(player_character& pc, ofstream &save);
void continue_game(player_character& pc, ifstream &get);
int main()
{
player_character pc;
ofstream save;
ifstream get;
//rest of main() goes here.
//pause screen
system("pause");
return 0;
}
//rest of functions go here.
void save_game(player_character &pc, ofstream &save_data)
{
save_data.open ("save.dat", ios::binary);
if (save_data.is_open())
{
save_data << "pc.type = " << pc.type << endl;
save_data << "pc.damage = " << pc.damage << endl;
save_data << "pc.stability = " << pc.stability << endl;
//doesn't work
//save_data.write(reinterpret_cast<char*>(&pc), sizeof(pc));
save_data.close();
}
else
{
cout << " Error. Unable to open file.";
}
}
void continue_game(player_character &pc, ifstream &get_data)
{
get_data.open("save.dat");
if (get_data.is_open())
{
//doesn't work
//get.read(reinterpret_cast<char*>(&pc), sizeof(pc));
get.close();
}
else
{
cout << " Error. Unable to open file.";
}
}
Thanks for the reponse. I'm trying the following revisions. The continue_game function appears to work. I recieve no errors (yet). When I select save after making a character I recieve the following error: Unhandled exception at 0x69197a28 in Undone.exe: 0xC0000005: Access violation reading location 0xffffffcc.
Google shows it as some sort of Windows issue.
Why is my function causing this error?
void save_game(ofstream &save, player_character const &pc)
{
save.open ("save.dat");
if (save.is_open())
{
save.write(reinterpret_cast<char const *>(pc.type), sizeof pc.type);
save.write(reinterpret_cast<char const *>(pc.damage), sizeof pc.damage);
save.write(reinterpret_cast<char const *>(pc.stability), sizeof pc.stability);
save.close();
}
else
{
cout << " Error. Unable to open file.";
}
}
int continue_game(ifstream &get)
{
if (!get.read(reinterpret_cast<char *>(&pc.type), sizeof pc.type)) { /* error */ }
if (!get.read(reinterpret_cast<char *>(&pc.damage), sizeof pc.damage)) { /* error */ }
if (!get.read(reinterpret_cast<char *>(&pc.stability), sizeof pc.stability)) { /* error */ }
return pc.type;
return pc.damage;
return pc.stability;
}
I've changed the save_game and continue_game code and included it here. I also collected the debugger and autos output (small version of autos, not all seven pages). Apparently my values are not being evaluated in save_game so continue_game has nothing to work with and doesn't cause errors.
Here's the code and debugger/autos printouts:
int save_game(ofstream &save, player_character& pc)
{
save.open ("save.dat", ios::binary);
if (save.is_open())
{
//the error hits here:
save.write(reinterpret_cast<char const *>(pc.type), sizeof pc.type);
save.write(reinterpret_cast<char const *>(pc.damage), sizeof pc.damage);
save.write(reinterpret_cast<char const *>(pc.stability), sizeof pc.stability);
save.close();
}
else
{
cout << " Error. Unable to open file.";
}
return pc.type;
return pc.damage;
return pc.stability;
}
int continue_game(ifstream &get, player_character& pc)
{
get.open ("save.dat", ios::binary);
if (get.is_open())
{
if (!get.read(reinterpret_cast<char *>(&pc.type), sizeof pc.type)) { /* error */ }
if (!get.read(reinterpret_cast<char *>(&pc.damage), sizeof pc.damage)) { /* error */ }
if (!get.read(reinterpret_cast<char *>(&pc.stability), sizeof pc.stability)) { /* error */ }
get.close();
}
else
{
cout << " Error. Unable to open file.";
}
return pc.type;
return pc.damage;
return pc.stability;
}
Debugger Output Window:
First-chance exception at 0x644e7a28 in Undone.exe: 0xC0000005: Access violation reading location 0xffffffcc.
Unhandled exception at 0x644e7a28 in Undone.exe: 0xC0000005: Access violation reading location 0xffffffcc.
Autos:
- pc {type='Ì' damage=-858993460 stability=-858993460 } player_character &
type -52 'Ì' char
damage -858993460 int
stability -858993460 int
pc.type -52 'Ì' char
- save {_Filebuffer={...} } std::basic_ofstream > &
+ std::basic_ostream > {...} std::basic_ostream >
+ _Filebuffer {_Pcvt=0x00000000 _Mychar='Ì' _Wrotesome=false ...} std::basic_filebuf >
Well I'vve managed to make some progress. I discovered I needed to place arguments into my main_menu function (mind you I'm not taking about the main() function, but one I made) so that they would be passed on to my save_game function. I was also able to stop the access error by adding an & into my write function.
So this:
save.write(reinterpret_cast<char const *>(&pc.type), sizeof pc.type);
save.write(reinterpret_cast<char const *>(&pc.damage), sizeof pc.damage);
save.write(reinterpret_cast<char const *>(&pc.stability), sizeof pc.stability);
instead of:
save.write(reinterpret_cast<char const *>(pc.type), sizeof pc.type);
save.write(reinterpret_cast<char const *>(pc.damage), sizeof pc.damage);
save.write(reinterpret_cast<char const *>(pc.stability), sizeof pc.stability);
The savve_game code still doesn't work properly yet when it comes to putting data into a file but it does printout to the screen.
You have to read and write each primitive data type, one by one, and use unformatted I/O only. Example:
void serialize(std::ostream & o, Player const & p)
{
o.write(reinterpret_cast<char const *>(p.type), sizeof p.type);
o.write(reinterpret_cast<char const *>(p.damage), sizeof p.damage);
o.write(reinterpret_cast<char const *>(p.stability), sizeof p.stability);
}
Player deserialize(std::istream & i)
{
Player p;
char pt;
int pd, ps;
if (!i.read(reinterpret_cast<char *>(&pt), sizeof pt)) { /* error */ }
if (!i.read(reinterpret_cast<char *>(&pd), sizeof pd)) { /* error */ }
if (!i.read(reinterpret_cast<char *>(&ps), sizeof ps)) { /* error */ }
p.type = pt; p.damage = pd; p.stability = ps;
return p;
}
Follow-up to the previous answer and edited question.
Judging by your continue_game signature,
int continue_game(ifstream &get)
pc must be some form of global struct that holds the character you're trying to resume. If pc is allocated at run-time, then you are using it incorrectly.
When you do this:
return pc.type;
return pc.damage;
return pc.stability;
A function can only have one return-type; only pc.type would be returned. This is not the cause of the error, but rather a design flaw you should be aware of.
Your initial approach to continue_game, wherein you passed in the relevant struct as a parameter,
void continue_game(player_character &pc, ifstream &get_data)
was a much better idea. I believe that if you combine the two you will find yourself without the error.
If the error continues, please use a debugger and paste the line. In addition, I can provide a sample of the code I described if you wish me to.
It looks like I figured it out! Here's what I've come up with: struct in array...and it appears to work just fine.
void save_game(fstream& file, player_type& pc)
{
file.open("save.dat");
if (file.is_open())
for (int i = 0; i < 1; i++)
{
file << pc.w_name << endl;
file << pc.d_rate << endl;
file << pc.s_rate << endl;
file.close();
}
else
cout << " Error. Unable to open file.";
menu(pc);
}//end save function
void continue_game(fstream& file, player_type player[])
{
file.open("save.dat");
if (file.is_open())
for (int i = 0; i < 1; i++)
{
file >> player[i].w_name;
file >> player[i].d_rate;
file >> player[i].s_rate;
file.close();
}
else
cout << " Error. Unable to open file.";
for (int i = 0; i < 1; i++)
{
cout << "You are continuing the game with the following weapon: " << player[i].w_name << endl;
cout << "Which has a damage rating of " << player[i].d_rate << " and a stability rating of " << player[i].s_rate << endl;
}
cout << endl;
menu(pc);
}//end continue_game