public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Pedro Franco de Carvalho <pedromfc@linux.vnet.ibm.com>
To: Ulrich Weigand <uweigand@de.ibm.com>
Cc: gdb-patches@sourceware.org
Subject: Re: [PATCH 3/4] Use get_remote_packet_size in download_tracepoint
Date: Mon, 25 Jun 2018 20:51:00 -0000	[thread overview]
Message-ID: <878t72mvxo.fsf@linux.vnet.ibm.com> (raw)
In-Reply-To: <20180625103720.2F6DAD801CC@oc3748833570.ibm.com>

Ulrich Weigand <uweigand@de.ibm.com> writes:

> You know from the beginning that the agent expression will take
> (2 * aexpr->len) bytes, so it should be OK to only check this
> once, ahead of time.  In fact, sending a partial agent expression
> seems to be worse than sending none, so if the agent expression
> is too long, I think it should be just omitted (and the user
> warned).

I don't think a partial agent expression would be sent in this case,
since this is before the first putpkt is called in the function. But I
can still issue the warning and ignore the condition expression instead
of failing on the assertion. Otherwise I can check the size once and
call a gdb_assert if its too small, like the rest of the function. Which
is better?

Would something like one of the two alternative below be ok for checking
the size only once?

The second one looks complicated, but my goal was to avoid overflows in
2 * aexpr->len, since that length ultimately comes from the condition
expression the user supplies.

I am also assuming throughout this function that size_t and
gdb::char_vector::size_type are compatible (since buf.size () returns
the latter and xsnprintf takes a size_t). Is this ok?

Thanks!

1:

       if (remote_supports_cond_tracepoints ())
 	{
 	  agent_expr_up aexpr = gen_eval_for_expr (tpaddr, loc->cond.get ());
-	  xsnprintf (buf + strlen (buf), BUF_SIZE - strlen (buf), ":X%x,",
-		     aexpr->len);
-	  pkt = buf + strlen (buf);
-	  for (int ndx = 0; ndx < aexpr->len; ++ndx)
-	    pkt = pack_hex_byte (pkt, aexpr->buf[ndx]);
-	  *pkt = '\0';
+
+	  int cond_str_size = snprintf (NULL, 0, ":X%x,", aexpr->len);
+	  gdb_assert (cond_str_size >= 0);
+
+	  cond_str_size += aexpr->len * 2;
+
+	  if (cond_str_size < buf.size () - strlen (buf.data ()))
+	    {
+	      sprintf (buf.data () + strlen (buf.data ()),
+		       ":X%x,", aexpr->len);
+
+	      pkt = buf.data () + strlen (buf.data ());
+
+	      for (int ndx = 0; ndx < aexpr->len; ++ndx)
+		pkt = pack_hex_byte (pkt, aexpr->buf[ndx]);
+	      *pkt = '\0';
+	    }
+	  else
+	    warning (_("Condition expression too long, "
+		       "ignoring tp %d cond"), b->number);
 	}
       else
 	warning (_("Target does not support conditional tracepoints, "

2:

       if (remote_supports_cond_tracepoints ())
 	{
 	  agent_expr_up aexpr = gen_eval_for_expr (tpaddr, loc->cond.get ());
-	  xsnprintf (buf + strlen (buf), BUF_SIZE - strlen (buf), ":X%x,",
-		     aexpr->len);
-	  pkt = buf + strlen (buf);
-	  for (int ndx = 0; ndx < aexpr->len; ++ndx)
-	    pkt = pack_hex_byte (pkt, aexpr->buf[ndx]);
-	  *pkt = '\0';
+
+	  int cond_str_size = snprintf (NULL, 0, ":X%x,", aexpr->len);
+	  gdb_assert (cond_str_size >= 0);
+
+	  size_t size_left = buf.size () - strlen (buf.data ());
+
+	  bool size_ok = (cond_str_size < size_left);
+
+	  if (size_ok)
+	    {
+	      size_left -= cond_str_size;
+
+	      size_ok = (size_left/2 > aexpr->len);
+
+	      if (size_ok)
+		{
+		  sprintf (buf.data () + strlen (buf.data ()),
+			   ":X%x,", aexpr->len);
+
+		  pkt = buf.data () + strlen (buf.data ());
+
+		  for (int ndx = 0; ndx < aexpr->len; ++ndx)
+		    pkt = pack_hex_byte (pkt, aexpr->buf[ndx]);
+		  *pkt = '\0';
+		}
+	    }
+
+	  if (!size_ok)
+	    warning (_("Condition expression too long, "
+		       "ignoring tp %d cond"), b->number);
 	}

  reply	other threads:[~2018-06-25 20:51 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-06-20 21:09 [PATCH 0/4] Allow larger sizes for tracepoint register masks Pedro Franco de Carvalho
2018-06-20 21:09 ` [PATCH 1/4] Fix indentation in remote_target::download_tracepoint Pedro Franco de Carvalho
2018-06-25 10:32   ` Ulrich Weigand
2018-06-20 21:09 ` [PATCH 4/4] Variable size for regs mask in collection list Pedro Franco de Carvalho
2018-06-25 10:38   ` Ulrich Weigand
2018-06-26 16:58   ` Pedro Alves
2018-06-26 18:52     ` Pedro Franco de Carvalho
2018-06-20 21:09 ` [PATCH 3/4] Use get_remote_packet_size in download_tracepoint Pedro Franco de Carvalho
2018-06-25 10:37   ` Ulrich Weigand
2018-06-25 20:51     ` Pedro Franco de Carvalho [this message]
2018-06-26 10:52       ` Ulrich Weigand
2018-06-26 16:53       ` Pedro Alves
2018-06-26 18:49         ` Pedro Franco de Carvalho
2018-06-20 21:10 ` [PATCH 2/4] Remove trailing '-' from the last QTDP action packet Pedro Franco de Carvalho
2018-06-25 10:33   ` Ulrich Weigand

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=878t72mvxo.fsf@linux.vnet.ibm.com \
    --to=pedromfc@linux.vnet.ibm.com \
    --cc=gdb-patches@sourceware.org \
    --cc=uweigand@de.ibm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).