Hello
If I try to format some data in a anligned string with Genode::snprintf(), the data is stored in the string, but my additional format specifier for the field width aren't respected.
My string is:
static const char* lineFormat = "%5d "%25s" "%25s" %10s %5d\r\n"
If I do want to right align the numbers (%-5d) the function crashes my program.
The comment on the functoin doesn't specify that format sizes arn't supported.
Best regards Pirmin Duss
Hello Pirmin,
If I try to format some data in a anligned string with Genode::snprintf(), the data is stored in the string, but my additional format specifier for the field width aren't respected.
My string is:
static const char* lineFormat = "%5d "%25s" "%25s" %10s %5d\r\n"
Genode's format-string functions are limited to the few use cases present in the base framework. I.e., it is sufficient for simple tasks like printing a number or concatenating strings but it is not meant as a full-fledged POSIX compliant formatting utility. If you need a complete format-string implementation, I recommend you to use the libc instead.
If I do want to right align the numbers (%-5d) the function crashes my program.
This should not happen. Thank you for reporting. I just opened an issue about it:
https://github.com/genodelabs/genode/issues/1485
The comment on the functoin doesn't specify that format sizes arn't supported.
I keep a note to cover this information in the upcoming documentation. For now, please let me refer you to the implementation. I think the code is pretty self-explanatory. If not, please don't hesitate to ask.
https://github.com/genodelabs/genode/blob/master/repos/base/src/base/console...
Best regards Norman
Hello Pirmin,
If I do want to right align the numbers (%-5d) the function crashes my program.
This should not happen. Thank you for reporting. I just opened an issue about it:
unfortunately, I am not able to reproduce the problem. When modifying the printf test using the attached patch, the program runs just fine.
Could you share a small test program that triggers the problem? That would be greatly appreciated.
Best regards Norman
Hello Norman,
Quoting Norman Feske <norman.feske@...1...>:
Hello Pirmin,
If I do want to right align the numbers (%-5d) the function crashes my program.
This should not happen. Thank you for reporting. I just opened an issue about it:
unfortunately, I am not able to reproduce the problem. When modifying the printf test using the attached patch, the program runs just fine.
Could you share a small test program that triggers the problem? That would be greatly appreciated.
I can share the code, where I had the problem, but it is not what I would call a small example, but it's also not that big.
It is in an older version of my TraceTool.
https://github.com/trimpim/TraceTool/blob/2f282a9bda1c32ed21019e1218ea6e3226...
In the method SubjectList::toString() the data from the list of subjects is stored in to a string buffer, that is later sent to a terminal session.
I'm currently updating the coding style of the whole code to match the Genode coding standard, and fix some big bugs.
As I think a full c++ string implementation is too mouch for my small problem, I have written a small string builder class, that stores the data in a Genode::String and addet the methods to format the data there.
Bast regards Pirmin
Best regards Norman
-- Dr.-Ing. Norman Feske Genode Labs
http://www.genode-labs.com · http://genode.org
Genode Labs GmbH · Amtsgericht Dresden · HRB 28424 · Sitz Dresden Geschäftsführer: Dr.-Ing. Norman Feske, Christian Helmuth
Hi Pirmin,
On 04/21/2015 07:39 PM, Duss Pirmin wrote:
I can share the code, where I had the problem, but it is not what I would call a small example, but it's also not that big.
It is in an older version of my TraceTool.
https://github.com/trimpim/TraceTool/blob/2f282a9bda1c32ed21019e1218ea6e3226...
In the method SubjectList::toString() the data from the list of subjects is stored in to a string buffer, that is later sent to a terminal session.
thank you for sharing the code. I could indeed reproduce a segmentation fault (I ran the program on Linux). I could observe that the symptom such as the fault address changed when I modified the program. For example, by not linking the libc, removing the call to Genode::snprintf, or moving a constructor implementation to a header file. This erratic change of symptoms generally hints at some kind of memory corruption. I successively simplified the program further and further while making sure that the problem persisted. In the end, I arrived at the situation [1]. This final program has actually nothing in common with your original program any longer but the kind of crash produced by it remains the same. With the simple test program, I could spot the bug.
The issue is apparently the C++11 list-style initialization of a reference member. In the constructor of the 'TerminalClient', the mrSubjectList member is initialized via the following construct:
mrSubjectList { rSubjectList }
This way, the compiler creates a temporary copy of the SubjectList and initializes the reference member with the temporary copy. This is of course not what you want because once the temporary copy disappears, the TerminalClient will keep working with a dangling reference. Instead, I presume that you intended to simply forward the reference. When using a normal C++ initializer, the program works as intended:
mrSubjectList ( rSubjectList )
As a precaution for issues like this, it may be helpful to disallow the copying of the SubjectList by letting the class inherit the Genode::Noncopyable class as found in [2]. This way, the compiler will detect such unintended copies.
[1] https://github.com/nfeske/genode/commit/1d78f58cb54d88c6d5da1290bb7449f0a170... [2] https://github.com/genodelabs/genode/blob/master/repos/base/include/util/non...
Cheers Norman
Hello,
when compiling Norman's test with the -Wextra command-line switch, GCC prints the following diagnostic hinting that the resulting binary may not behave as expected.
c.cc: In constructor ‘Object_with_ref::Object_with_ref(const Wrapped_value&)’: c.cc:42:14: warning: a temporary bound to ‘Object_with_ref::_value’ only persists until the constructor exits [-Wextra]
Regards
Hello Norman
Many thanks for finding my problem.
As I have fixed this now, an other bug I was hunting also was fixed ;-).
Best regards Pirmin