Skip to content

Conversation

@ifplusor
Copy link
Contributor

@ifplusor ifplusor commented Jan 19, 2019

ISSUE #71 ,MQMessage::getProperty return a dangling reference when returning an empty string.

@ShannonDing ShannonDing added the bug Something isn't working label Jan 21, 2019
@ShannonDing ShannonDing added this to the 1.2.1 milestone Jan 21, 2019
@ShannonDing ShannonDing changed the title Fixed: return dangling reference. [ISSUE #71]Fixed the issue that dangling reference returned when get message property . Jan 21, 2019
@ShannonDing ShannonDing changed the title [ISSUE #71]Fixed the issue that dangling reference returned when get message property . [ISSUE #71]Fixed the issue that dangling reference returned when getting message property . Jan 21, 2019
m_properties[name] = value;
}

const string & MQMessage::getProperty(const string& name) const {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ifplusor hi, if we can change getProperty return string but not string& ? because i don't think there is a need to return reference string for getProperty

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jonnxu I think 'const string&' is ok. Here, we need return a read-only object or a new one, and return a reference don't allocate a new object.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think both of them are fine.
It will not affect efficiency too much.

Copy link
Contributor

@jovany-wang jovany-wang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I left a few comments.

}else{
if (it == m_properties.end()) {
return EMPTY_STRING;
} else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice catch for the code style.


const string MQMessage::KEY_SEPARATOR = " ";

static const string EMPTY_STRING = "";
Copy link
Contributor

@jovany-wang jovany-wang Jan 21, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think defining an empty str as a static global var is the best approach.
Maybe we could add a new file constant.h like this:

class Constants {
public:
    static const string EMPTY_STRING = "";
};

Any way, we should avoid to use global variables and global static variables.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jovany-wang the visibility of global static variables is only in this file. i think depend on other file is unnecessary for only this one. if we use EMPTY_STRING in other files, it's ok.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you don't want to move it to constant.h, let's use return ""; directly?
Because I don't think we should use a global static variable in such case.:)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you don't want to move it to constant.h, let's use return ""; directly?
Because I don't think we should use a global static variable in such case.:)

@jovany-wang now, we return a reference, and return a temp object is a bug.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ifplusor Thanks, I see.
So if the method may be return a temp variable, it is pretty make sense to declare it as string MQMessage::getProperty(const string& name);.

If the method always return his member variables, we could return a const ref.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jovany-wang i don't change method signature, we can discuss it.

@jovany-wang
Copy link
Contributor

Thanks. Let's merge this PR as a workaround approach first.

@ShannonDing ShannonDing merged commit 1642a3a into apache:master Jan 22, 2019
@ifplusor ifplusor deleted the fixed-dangling-reference branch January 23, 2019 01:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants