From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS47066 71.19.144.0/20 X-Spam-Status: No, score=-1.9 required=3.0 tests=AWL,BAYES_00, MSGID_FROM_MTA_HEADER shortcircuit=no autolearn=unavailable version=3.3.2 Path: news.gmane.org!not-for-mail From: Eric Wong Newsgroups: gmane.comp.lang.ruby.clogger.general Subject: [PATCH 4/6] prevent potential premature GC in byte_xs Date: Sat, 15 Feb 2014 23:09:06 +0000 Message-ID: <1392505748-13740-4-git-send-email-normalperson@yhbt.net> References: <1392505748-13740-1-git-send-email-normalperson@yhbt.net> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Trace: ger.gmane.org 1392505769 7139 80.91.229.3 (15 Feb 2014 23:09:29 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sat, 15 Feb 2014 23:09:29 +0000 (UTC) To: clogger@librelist.org Original-X-From: clogger@librelist.org Sun Feb 16 00:09:36 2014 Return-path: Envelope-to: gcrcg-clogger@m.gmane.org List-Archive: List-Help: List-Id: List-Post: List-Subscribe: List-Unsubscribe: Precedence: list Original-Sender: clogger@librelist.org Xref: news.gmane.org gmane.comp.lang.ruby.clogger.general:68 Archived-At: Received: from zedshaw2.xen.prgmr.com ([71.19.156.177]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1WEoMZ-00015h-H0 for gcrcg-clogger@m.gmane.org; Sun, 16 Feb 2014 00:09:35 +0100 Received: from zedshaw2.xen.prgmr.com (unknown [IPv6:::1]) by zedshaw2.xen.prgmr.com (Postfix) with ESMTP id D7B1475019 for ; Sat, 15 Feb 2014 23:18:38 +0000 (UTC) If we convert an object to string, there is a potential the compiler may optimize away the converted string if escaping is needed. Prevent that with RB_GC_GUARD. --- ext/clogger_ext/clogger.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/ext/clogger_ext/clogger.c b/ext/clogger_ext/clogger.c index ed01534..8b05c34 100644 --- a/ext/clogger_ext/clogger.c +++ b/ext/clogger_ext/clogger.c @@ -166,10 +166,11 @@ static inline int need_escape(unsigned c) } /* we are encoding-agnostic, clients can send us all sorts of junk */ -static VALUE byte_xs_str(VALUE from) +static VALUE byte_xs(VALUE obj) { static const char esc[] = "0123456789ABCDEF"; unsigned char *new_ptr; + VALUE from = rb_obj_as_string(obj); const unsigned char *ptr = (const unsigned char *)RSTRING_PTR(from); long len = RSTRING_LEN(from); long new_len = len; @@ -203,14 +204,10 @@ static VALUE byte_xs_str(VALUE from) } assert(RSTRING_PTR(rv)[RSTRING_LEN(rv)] == '\0'); + RB_GC_GUARD(from); return rv; } -static VALUE byte_xs(VALUE from) -{ - return byte_xs_str(rb_obj_as_string(from)); -} - static void clogger_mark(void *ptr) { struct clogger *c = ptr; -- 1.9.0.rc3.13.gda73b5f