TEST_NLATTR_OBJECT and TEST_NLATTR_ARRAY fault when sizeof(obj) - 1 > DEFAULT_STRLEN

Dmitry V. Levin ldv at altlinux.org
Mon Jul 3 19:23:49 UTC 2017


On Mon, Jul 03, 2017 at 08:05:42AM +0800, JingPiao Chen wrote:
> On Mon, Jul 03, 2017 at 01:22:52AM +0300, Dmitry V. Levin wrote:
> > On Sun, Jul 02, 2017 at 12:12:54PM +0800, JingPiao Chen wrote:
> > > when sizeof(obj) - 1 > DEFAULT_STRLEN, we print all string,
> > > but strace print "STR"..., so the test fault.
> > > This appear when I test packet_diag_msg PACKET_DIAG_MCLIST.
> > 
> > Does this patch solve it?
> 
> Can solve it.
> > 
> > --- a/tests/test_nlattr.h
> > +++ b/tests/test_nlattr.h
> > @@ -99,20 +99,19 @@ print_nlattr(const unsigned int nla_len, const char *const nla_type)
> >  			   init_msg_, print_msg_,			\
> >  			   nla_type_, pattern_, obj_, ...)		\
> >  	do {								\
> > +		const size_t plen = sizeof(obj_) - 1 > DEFAULT_STRLEN	\
> > +			? DEFAULT_STRLEN : sizeof(obj_) - 1;		\
> >  		/* len < sizeof(obj_) */				\
> >  		TEST_NLATTR_((fd_), (nlh0_), (hdrlen_),			\
> >  			(init_msg_), (print_msg_),			\
> >  			(nla_type_), #nla_type_,			\
> > -			sizeof(obj_) - 1,				\
> > -			(pattern_), sizeof(obj_) - 1,			\
> > -			printf("\"%.*s\"",				\
> > -			(int) sizeof(obj_) - 1,	(pattern_)));		\
> > +			plen, (pattern_), plen,				\
> > +			printf("\"%.*s\"", (int) plen, (pattern_)));	\
> >  		/* short read of sizeof(obj_) */			\
> >  		TEST_NLATTR_((fd_), (nlh0_), (hdrlen_),			\
> >  			(init_msg_), (print_msg_),			\
> >  			(nla_type_), #nla_type_,			\
> > -			sizeof(obj_),					\
> > -			(pattern_), sizeof(obj_) - 1,			\
> > +			sizeof(obj_), (pattern_), plen,			\
> 
> I think here not need to replace with plen.
> So can we named str_len instead of plen?

OK, changed to keep the same size.

btw, I don't think printing unrecognized data as string is useful.
After all, most likely it's not a string in the first place.
Don't you think that printing it in hex (with QUOTE_FORCE_HEX flag set)
would be more useful?

> >  			printf("%p",					\
> >  			       RTA_DATA(NLMSG_ATTR(nlh, (hdrlen_)))));	\
> >  		/* sizeof(obj_) */					\
> > @@ -128,15 +127,15 @@ print_nlattr(const unsigned int nla_len, const char *const nla_type)
> >  			  init_msg_, print_msg_,			\
> >  			  nla_type_, pattern_, obj_, print_elem_)	\
> >  	do {								\
> > +		const size_t plen =					\
> > +			sizeof((obj_)[0]) - 1 > DEFAULT_STRLEN		\
> > +			? DEFAULT_STRLEN : sizeof((obj_)[0]) - 1;	\
> >  		/* len < sizeof((obj_)[0]) */				\
> >  		TEST_NLATTR_((fd_), (nlh0_), (hdrlen_),			\
> >  			(init_msg_), (print_msg_),			\
> >  			(nla_type_), #nla_type_,			\
> > -			sizeof((obj_)[0]) - 1,				\
> > -			(pattern_), sizeof((obj_)[0]) - 1,		\
> > -			printf("\"%.*s\"",				\
> > -			       (int) sizeof((obj_)[0]) - 1,		\
> > -			       (pattern_)));				\
> > +			plen, (pattern_), plen,				\
> > +			printf("\"%.*s\"", (int) plen, (pattern_)));	\
> >  		/* sizeof((obj_)[0]) < len < sizeof(obj_) */		\
> >  		TEST_NLATTR_((fd_), (nlh0_), (hdrlen_),			\
> >  			(init_msg_), (print_msg_),			\
> > 
> 
> This patch require the caller define DEFAULT_STRLEN,
> can we define DEFAULT_STRLEN in test_nlattr.h or tests.h?
> Why we define DEFAULT_STRLEN dispersedly?

Moved DEFAULT_STRLEN to tests.h, thanks for the hint.


-- 
ldv
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 801 bytes
Desc: not available
URL: <http://lists.strace.io/pipermail/strace-devel/attachments/20170703/cae630b3/attachment.bin>


More information about the Strace-devel mailing list