From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 shortcircuit=no autolearn=unavailable version=3.3.2 X-Original-To: normalperson@yhbt.net Received: from zedshaw2.xen.prgmr.com (zedshaw2.xen.prgmr.com [71.19.156.177]) by dcvr.yhbt.net (Postfix) with ESMTP id 8B1CC633819 for ; Sun, 28 Dec 2014 02:28:08 +0000 (UTC) Received: from zedshaw2.xen.prgmr.com (unknown [IPv6:::1]) by zedshaw2.xen.prgmr.com (Postfix) with ESMTP id 35DC374F8C for ; Sun, 28 Dec 2014 02:28:41 +0000 (UTC) MIME-Version: 1.0 Date: Sun, 28 Dec 2014 02:27:55 +0000 From: Eric Wong List-Archive: List-Help: List-Id: List-Post: List-Subscribe: List-Unsubscribe: Message-Id: <1419733675-6323-2-git-send-email-normalperson@yhbt.net> Precedence: list References: <1419733675-6323-1-git-send-email-normalperson@yhbt.net> Sender: sleepy.penguin@librelist.com Subject: [sleepy.penguin] [PATCH] inotify: cleanup Inotify::Event creation To: sleepy.penguin@librelist.com Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit We'll prefer using rb_str_new2 instead of rb_str_new(...,strlen) to save binary size. While we're at it, explain why we cannot take e->len into account for plain-old rb_str_new. --- ext/sleepy_penguin/inotify.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/ext/sleepy_penguin/inotify.c b/ext/sleepy_penguin/inotify.c index 1f2d4ac..b324227 100644 --- a/ext/sleepy_penguin/inotify.c +++ b/ext/sleepy_penguin/inotify.c @@ -124,8 +124,13 @@ static VALUE event_new(struct inotify_event *e) VALUE cookie = UINT2NUM(e->cookie); VALUE name; - /* name may be zero-padded, so we do strlen() */ - name = e->len ? rb_str_new(e->name, strlen(e->name)) : Qnil; + /* + * e->name is zero-padded, so we may use rb_str_new2. + * We do not use rb_str_new(e->name, e->len) because + * e->len counts all \0 padding bytes, and there may be + * multiple padding bytes + */ + name = e->len ? rb_str_new2(e->name) : Qnil; return rb_struct_new(cEvent, wd, mask, cookie, name); } -- EW