Opened 14 years ago

Closed 12 years ago

#2024 closed Bugs (wontfix)

boost::format("%p") % (uint8_t *) str is not like printf("%p", str)

Reported by: Geoff Barrett <gbarrett@…> Owned by: Samuel Krempp
Milestone: To Be Determined Component: format
Version: Boost 1.34.0 Severity: Problem
Keywords: Cc:

Description

I'm actually on 1.33.1 still but I can't see any difference that fixes this in the latest code.

The boost::format gets pointers to 8-bit types wrong. This because the underlying type is indistinguishable from a string. In this case, I think the formatting needs to take the hint from the format string about how to interpret the type.

I have a fix which is slightly hacky. Instead of passing the printf parameter directly to %, it can be wrapped in a container whose insertion method inspects the stream flags to see whether any of the basefield bits are set and only prints like a char/string if none of them are. The wrapper type is in the attachment. The format parsing needs to clear the basefield bits. Here is the diff from format/parsing.hpp:

321d320 < fpar->fmtstate_.flags_ &= ~std::ios_base::basefield; 326d324 < fpar->fmtstate_.flags_ &= ~std::ios_base::basefield;

Attachments (1)

format_printf.cc (4.7 KB ) - added by Geoff Barrett <gbarrett@…> 14 years ago.

Download all attachments as: .zip

Change History (3)

by Geoff Barrett <gbarrett@…>, 14 years ago

Attachment: format_printf.cc added

comment:1 by Marshall Clow, 13 years ago

This still a problem in 1.42:

#include <iostream>
#include <boost/format.hpp>
#include <cstdlib>

int main ( int argc, char *argv[] ) {
	const char *foo = "Hi Mom!";
	std::cout << str ( boost::format ( "%p" ) % foo ) << std::endl;
	printf ( "%p\n", foo );
	return 0;
	}

prints two different lines.

comment:2 by Marshall Clow, 12 years ago

Resolution: wontfix
Status: newclosed

My copy of the C99 standard (n869) says about "%p":

p The argument shall be a pointer to void. The value of the pointer is converted to a sequence of printing characters, in an implementation-defined manner.

Given that, I don't see any way that Boost.Format can "match what printf prints".

Note: See TracTickets for help on using tickets.