Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compiler warning due to different string alignments in mqueue.cpp code #591

Closed
oPaveo opened this issue Feb 24, 2018 · 5 comments
Closed
Labels

Comments

@oPaveo
Copy link

oPaveo commented Feb 24, 2018

I use Visual Studio 2017 (msvc15) under Windows 10 and Crypto++ 6.10.
I get the following linker warning:

Warning C4742: '`string'' has different alignment in '\cryptopp\mqueue.cpp' and 'foo\bar.cpp': 1 and 2

I know that is the Microsoft compiler problem and I reported it to them.
But I also know why It occures and there is a very simple way to avoid it.

In your code, in mqueue.cpp we have

return Output(1, (const byte *)"\0", 1, 0, blocking) != 0;

The string "\0" is actually a two-element array of two zero bytes - exactly the same as L"". So the problem is whether it is a 1-length char* or an empty wide string.

Maybe you can write
return Output(1, (const byte *)"\0\0", 1, 0, blocking) != 0;
or
return Output(1, (const byte *)"", 1, 0, blocking) != 0;

Both codes works the same way and without linker warnings.

P.S. Older versions of Crypto++ give the same effect, of course.

@noloader
Copy link
Collaborator

noloader commented Feb 24, 2018

Thanks @oPaveo.

Yeah, we want to be as warning free as possible. That warning should be contained. It's a bug on our part.

It looks like you are taking about this function:

bool EqualityComparisonFilter::HandleMismatchDetected(bool blocking)
{
	m_mismatchDetected = true;
	if (m_throwIfNotEqual)
		throw MismatchDetected();
	return Output(1, (const byte *)"\0", 1, 0, blocking) != 0;
}

And Output from filters.h

size_t Output(int outputSite, const byte *inString, size_t length, int messageEnd, bool blocking, const std::string &channel=DEFAULT_CHANNEL);

How do you feel about doing away with the string literal altogether. Maybe something like:

bool EqualityComparisonFilter::HandleMismatchDetected(bool blocking)
{
	m_mismatchDetected = true;
	if (m_throwIfNotEqual)
		throw MismatchDetected();

        // https://github.com/weidai11/cryptopp/issues/591
        const byte b[1] = {0};
	return Output(1, b, 1, 0, blocking) != 0;
}

@noloader noloader added the Bug label Feb 24, 2018
@oPaveo
Copy link
Author

oPaveo commented Feb 24, 2018

Hello,
Your solution looks very well. I tested it in my environment and it is also warning free :-)
Thanks.

@noloader
Copy link
Collaborator

Thanks @oPaveo,

Do you want to make a PR, or would you like us to handle it?

@oPaveo
Copy link
Author

oPaveo commented Feb 24, 2018

I prefer to leave it to you.

@noloader noloader changed the title Linker has different string alignments beacause of mqueue.cpp code. Compiler warning due to different string alignments in mqueue.cpp code Feb 24, 2018
@noloader
Copy link
Collaborator

Thanks again @oPaveo.

Cleared at Commit 53ccd310b8b3.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants