creating an object from a template based on user input - c++

I am writing an application using the crypto++ library. For those not familiar with it, the ECB_Mode template classes inherit from CipherModeBase. The program compiles and runs, but the output I am getting is incorrect. When I call the encryption method from the cipher_object, it doesn't work the same way as if I use an ECB_Mode object directly. I have verified that the options object instance variables are getting assigned correctly. I would like to create the instances within an if_then_else structure or switch_case so I can keep the code nice and DRY. What am I doing wrong?
Here is what I am trying but doesn't work:
CipherModeBase *cipher_object;
cipher_object == NULL;
if(options->cipher == BS_AES)
{
ECB_Mode<AES >::Encryption ecbEncryption(options->key, options->keylen);
cipher_object = &ecbEncryption;
}
else if(options->cipher == BS_TWOFISH)
{
ECB_Mode<Twofish >::Encryption ecbEncryption(options->key, options->keylen);
cipher_object = &ecbEncryption;
}
cipher_object->processData(args);
Here is what does work:
ECB_Mode<AES >::Encryption ecbEncryption(options->key, options->keylen);
ecbEncryption.processData(args);
PS. I know not to use ECB mode. I just don't want to mess with IVs until I can get everything working. I am also relatively inexperienced with C++.

Your ecbEncryption objects are declared on the stack within the if and else scopes. (Scopes are the things enclosed by curly brackets).
An object will be destroyed when the scope it was declared in exits. So, the object you're calling processData on has been deleted before you call that method. Clearly that won't work.
One option is you could declare the objects on the heap instead of the stack. That way the lifetime could be controlled to work the way you want.
Try to use a std::unique_ptr for the cipher_object instead of a raw pointer. Then, in the if and else clauses assign to it like:
cipher_object.reset( new ECB_Mode<AES>::Encryption(options->key, options->keylen) );
Then the object will remain on the heap until the end of the cipher_object's scope, at which point the unique_ptr will delete it for you. And, the cipher_object's scope will last until after you call any methods on it.

Here:
cipher_object == NULL;
if(options->cipher == BS_AES)
{
// v
ECB_Mode<AES >::Encryption ecbEncryption(options->key, options->keylen);
cipher_object = &ecbEncryption;
}
ecbEncryption is local to that scope. You are storing the address of a local, and using it after it goes out of scope. That's undefined behaviour. You should allocate it on the heap using the new keyword:
if(options->cipher == BS_AES)
{
cipher_object = new ECB_Mode<AES >::Encryption(options->key, options->keylen);
}
You should do the same for the other if statement.
Also note that this:
cipher_object == NULL;
Should be changed to this:
cipher_object = NULL;
The problem should be solved using the above code, though.

Related

Qt - Function returns pointer, that gets deleted

I have a function which returns a pointer to a local variable, which I used new on to allocate to the heap. However, as soon as the return pointer is used, it causes a seg fault.
IConnectionDesc *ConnectionDialog::uiToConnection(IConnectionDesc *conDesc)
{
_lrtrace();
IConnectionDesc *result = nullptr;
if (conDesc)
result = conDesc;
else
result = new ConnectionDesc();
result->setName(ConnectionDesc::connectionNameForReport(ui->leConnectionName->text()));
result->setAutoconnect(ui->cbAutoConnect->isChecked());
result->setKeepDBCredentials(!ui->cbbKeepCredentials->isChecked());
if (ui->tabWidget->currentIndex() == 0)
{
ConnectionDesc *lrresult = new ConnectionDesc();
lrresult->createFrom(result);
lrresult->setHost(ui->leServerName->text());
lrresult->setPort(ui->lePort->text());
lrresult->setDriver(ui->cbbDrivers->currentText());
lrresult->setUserName(ui->leUserName->text());
lrresult->setPassword(ui->lePassword->text());
lrresult->setDatabaseName(ui->leDataBase->text());
_lrendtrace();
return lrresult;
}
else
{
HBOConnectionDesc *hboresult = new HBOConnectionDesc();
hboresult->createFrom(result);
hboresult->setApplication(ui->cbbhboapplication->currentText());
hboresult->setService(ui->cbbhboservice->currentText());
_lrendtrace();
return hboresult;
}
}
IConnectionDesc is an interface, which inherits from QObject, ConnectionDesc and HBOConnectionDesc both inherit from IConnectionDesc. createFrom just copies the shared properties into the instance. This kind of issue occurs in bigger functions, where a lot more is shared too, hence why I'd prefer to keep the single instances of result->setName(
I've tried multiple different versions of this, but either Qt's Q_DISABLE_COPY or a Segementation fault cause an issue. I have no idea what else to do.
I cannot make the function simply edit the provided conDesc, because its optional, and could be a null pointer itself.

Deleting and Reassigning a pointer from QList<Object *> to new Object using Iterator, is this right?

I have a QList and I'm trying to replace the objects in the list with new objects. The context is that I have a list of custom objects (the class name is "Conversation") to represent a list of group chats in a messaging platform. I use std::find_if to iterate through the list of pointers to find one with the right ID, and I want to take the pointer to that found object, deallocate it (delete?), and reassign that pointer to point at an object I generate with the "new" keyword. I think I'm doing this right but I'm not sure how to verify.
I tried a couple different iterations, ran into some issues where I realized I was using a const_iterator rather than just an iterator, so I couldn't modify any data. But I've fixed that and it seems like it's working, but I'm not positive.
Here's what I've got:
GroupChat *gc = new GroupChat(); // extends Conversation
// ...I update the member data here...
auto foundChat = std::find_if(conversations_.Conversations.begin2(),
conversations_.Conversations.end2(),
[this, gc](Conversation* o) { // my code to find the correct one...
}
if (foundChat != conversations_.Conversations.end()) {
auto c = (*foundChat);
delete c; // Is this right? Not positive...
//*foundChat = nullptr; // do I need this?
c = gc;
}
It seems like it's working but I'm worried about dangling pointers and incorrect memory deallocation/allocation. Could someone spot check me on this? Thanks for any help!

Optional instance of a class

Hi I am trying to do something like the follow.
If have some variables passed from cmd line i.e..
const char * outputtype1
const char * outputtype2
The latter to can be NULL.
I then want to create an instance of a class if outputtype2 is set in command line. How can I have the instance of this class optional. i.e..
if(outputtype2)
{
cats thomas(outputtype2);
}
I then use this later like
thomas.eatfood(whiskers);
This is where it gets upset.Obviously thomas doesnt exist if outputtype2 is null, but why cant I just do this?
if (outputtype2)
{
cats thomas(outputtype2);
}
without error
'thomas' was not declared in this scope. I fear I am missing some fundamental rule here.
You'd probably want boost::optional<cats>. This allows you to define thomas up front, and assign cats(outputtype2) to thomas if and only if it's available.
The consequence is that on any use of thomas, you will have to check it was actually assigned to.
If I understand your question, the problem is that you create the instance of the class in the if scope. Later you try to call the method eatfood of the object thomas but the object doesn't exist in the current scope.
Maybe you want to do this...
if (outputtype2) {
cats thomas(outputtype1);
thomas.eatfood(whiskers);
}
Or use a pointer...
Cats* thomas = NULL;
if (outputtype2) {
thomas = new Cats(outputtype1);
}
if (thomas != NULL) {
thomas->eatfood(whiskers);
}
What you miss is the concept of variable scope and visibility.
When you write :
if(outputtype2)
{
cats thomas(outputtype2);
}
that means that the variable thomas exists only in the block it is declared in. Outside it, thomas doesn't exists anymore, both the object and the reference are destroyed when the control leaves the block. So you cant use them outside!
Find another logic to write your program.

How to know whether the TStringList was newed

I am using C++ Builder XE4 on Windows7 Professional (32bit).
I have a question on TStringList;
What I would like to do is to know whether the TStringList variable was newed or not, to prevent Add() method to non-newed TStringList;
I thought checking NULL might work, but it didn't.
TStringList *list;
// list = new TStringList(); // someone commented out by mistakes
if (list == NULL) {
ShowMessage(L"NULL");
} else {
ShowMessage(L"not empty");
}
return;
The above code shows "not empty" dialog.
What is a standard way to check the newed or non-newed TStringList?
It is very commonplace is c/c++ to initialize pointers to NULL, and have them be null whenever they aren't pointing at something valid.
As such, your if statement is a very commonplace and appropriate piece of code.
However, this does require initializing your variable.
Unless declared at global/file scope, the line: TStringList *list; does not initialize the variable to any specific value - this leaves it pointing at random crap.
You need to change it to TStringList *list = NULL; (or similar, some people dont like "NULL")

Calling new multiple times within same for loop

Here is a quick snippet of my code to parse PDB files for molecular dynamics simulations:
Structure *s = new Structure(pdb_filename);
Chain *c = new Chain();
while( ... read file ... ) {
if ( ... new chain ... ) {
Chain *c = new Chain();
s->add_child(c); // Add reference to a vector to
// save the Chain for later
}
}
When the containing function is called, the code acts as normal and gives brand new Structure and Chain objects as in the first two lines of the snippet.
When the criteria for a new chain is met again while looping over the file, the code returns the same Chain pointer to the object as before.
Will g++ give the same pointer over and over? Is there any way to get around this?
If I add the c pointer to the 's' children vector, I assume calling delete c will cause even larger headaches?
Structure *s = new Structure(pdb_filename);
Chain *c = new Chain();
You just defined c here
while( ... read file ... ) {
if ( ... new chain ... ) {
Chain *c = new Chain();
You just defined c here again shadowing the other one.
When the criteria for a new chain is met again while looping over the
file, the code returns the same Chain pointer to the object as before.
Now which one of the two you're looking at?
I think you can see the problem already.
the code returns the same Chain pointer to the object as before
That is hard to believe.
Will g++ give the same pointer over and over?
No. Each time you call new Chain it's a different Chain. It's a different pointer, pointing to different memory (that's why it's new). It's also shadowing the c before the while.
new Chain() should give a different pointer each time
I think I may have an inkling what your problem is--
The c within your loop does not refer to the same variable as the c outside your loop. The c outside your loop will retain its same value all the way through.