Skip to content

Next fix vsnprintf#365

Merged
bendudson merged 12 commits intoboutproject:v4.0.0-RCfrom
dschwoerer:next-fix-vxnprintf
Dec 1, 2016
Merged

Next fix vsnprintf#365
bendudson merged 12 commits intoboutproject:v4.0.0-RCfrom
dschwoerer:next-fix-vxnprintf

Conversation

@dschwoerer
Copy link
Contributor

Inlike the PR #319 (the PR for the master branch) this should also work for the derived exceptions.

bendudson and others added 8 commits October 13, 2016 17:38
Safe version, which guards against buffer overruns
by specifying the length of the buffer into which text
is being printed.

This was used in both Output and BoutException classes
to provide C printf-style output.
Missed a couple of instances in BoutException
derived classes.
Both Output and BoutException now use a static const int
BUFFER_LEN to control the length of the buffer used for
C style output.
Copy link
Member

@ZedThree ZedThree left a comment

Choose a reason for hiding this comment

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

I think only the ShiftOutput thing definitely needs to be fixed, but the dummy argument is a little concerning.

Also, I'm not sure about the name. Maybe bout_vsnprintf, or something, instead?

#include "formatfactory.hxx"

Datafile::Datafile(Options *opt) : parallel(false), flush(true), guards(true), floats(false), openclose(true), enabled(true), shiftOutput(false), file(NULL) {
Datafile::Datafile(Options *opt) : parallel(false), flush(true), guards(true), floats(false), openclose(true), enabled(true), file(NULL) {
Copy link
Member

Choose a reason for hiding this comment

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

This removes shiftOutput(false) from the initializer list. I assume this is accidental?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, thanks

/// reallocated.
/// len: the length of said buffer. May be changed, mussn't be const.
/// fmt: the const char * descriping the format.
/// va: A dummy argument. will
Copy link
Member

Choose a reason for hiding this comment

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

Is the dummy argument needed? It looks confusing when the macro is called.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

you are right, removed it 👍

}

throw BoutException("Error in field: %s", err_buffer);
delete[] err_buffer;
Copy link
Member

Choose a reason for hiding this comment

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

This will never be hit, and presumably leads to a memory leak if the exception isn't caught?
I'm not really sure a) if this is a problem and b) if so, how to fix it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This leads to a memory leak, no matter if caught or not. (not caught however doesn't really matter)
b) use std::string

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added BoutException(std::string) in order to avoid this issue

@ZedThree
Copy link
Member

Maybe in future it would be possible to replace all this C stuff with nice C++ idioms? This library looks like a really nice replacement for the printf family. It could possibly reduce a lot of the complexity about formatting messages.

@dschwoerer
Copy link
Contributor Author

Does "this library" (fmt?) support traditional formatting like %-10.3g?
This is the main reason why I don't like the C++ way - it is to bloated for number printing ...

@ZedThree
Copy link
Member

From what I can tell, it supports not only traditional printf style printing like that, but also python style:

 fmt::format("The answer is {}", 42);

@dschwoerer
Copy link
Contributor Author

dschwoerer commented Nov 14, 2016

Only the python format style - python also supports c-printf like statements ...( as the one above, left centered, at least 10 chars, and 3 digits after the point)

Update: sorry, I missed 'not only'.
From the description I didn't saw a single example for that, so I am not sure how good the floating point number support is ...

@ZedThree
Copy link
Member

Yep, look at fmt::printf, fmt::fprintf and fmt::sprintf:

 fmt::printf("The answer is %d\n", 42);

Also, see the SO answer here. Looks like we can do all this in pure C++, but still use printf format strings. Everyone's happy! :)

I'll have a bash at it tomorrow.

@ZedThree
Copy link
Member

Is this still needed for v4.0.0-RC? It looks quite similar to what is in there now - I think there was also a PR against master?

Also, is it possible that this functionality could be done using C++11 features instead of macros, e.g. std::snprintf? See http://stackoverflow.com/a/26221725/2043465

@dschwoerer
Copy link
Contributor Author

Using variadic templates requires that all this is defined in header files ...

I cannot see that this is already fixed in v4 RC:
https://github.com/boutproject/BOUT-dev/blob/v4.0.0-RC/src/sys/boutexception.cxx#L80

@bendudson
Copy link
Contributor

bendudson commented Nov 28, 2016 via email

@bendudson
Copy link
Contributor

To avoid best becoming the enemy of better, I vote we merge this for now, and then look at changing to a C++ solution later.
Can you (David) please change the base to v4.0.0-rc?

@dschwoerer dschwoerer changed the base branch from next to v4.0.0-RC December 1, 2016 15:15
@dschwoerer
Copy link
Contributor Author

base changed + conflicts resolved:
should be ready to merge ...

@bendudson bendudson merged commit af451f3 into boutproject:v4.0.0-RC Dec 1, 2016
@dschwoerer dschwoerer deleted the next-fix-vxnprintf branch February 27, 2018 01:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants