Return-Path: X-Original-To: notmuch@notmuchmail.org Delivered-To: notmuch@notmuchmail.org Received: from localhost (localhost [127.0.0.1]) by olra.theworths.org (Postfix) with ESMTP id 76588431FAF for ; Thu, 20 Dec 2012 18:04:03 -0800 (PST) X-Virus-Scanned: Debian amavisd-new at olra.theworths.org X-Spam-Flag: NO X-Spam-Score: -0.7 X-Spam-Level: X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5 tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled Received: from olra.theworths.org ([127.0.0.1]) by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id pBTTMiRn5gUr for ; Thu, 20 Dec 2012 18:04:03 -0800 (PST) Received: from dmz-mailsec-scanner-4.mit.edu (DMZ-MAILSEC-SCANNER-4.MIT.EDU [18.9.25.15]) by olra.theworths.org (Postfix) with ESMTP id CFB6E431FAE for ; Thu, 20 Dec 2012 18:04:02 -0800 (PST) X-AuditID: 1209190f-b7f016d000000e07-6d-50d3c39134a2 Received: from mailhub-auth-4.mit.edu ( [18.7.62.39]) by dmz-mailsec-scanner-4.mit.edu (Symantec Messaging Gateway) with SMTP id FA.64.03591.193C3D05; Thu, 20 Dec 2012 21:04:01 -0500 (EST) Received: from outgoing.mit.edu (OUTGOING-AUTH.MIT.EDU [18.7.22.103]) by mailhub-auth-4.mit.edu (8.13.8/8.9.2) with ESMTP id qBL240r2027304; Thu, 20 Dec 2012 21:04:00 -0500 Received: from awakening.csail.mit.edu (awakening.csail.mit.edu [18.26.4.91]) (authenticated bits=0) (User authenticated as amdragon@ATHENA.MIT.EDU) by outgoing.mit.edu (8.13.6/8.12.4) with ESMTP id qBL23w0s003488 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES128-SHA bits=128 verify=NOT); Thu, 20 Dec 2012 21:03:59 -0500 (EST) Received: from amthrax by awakening.csail.mit.edu with local (Exim 4.80) (envelope-from ) id 1Tlrxu-0007Ku-IZ; Thu, 20 Dec 2012 21:03:58 -0500 Date: Thu, 20 Dec 2012 21:03:58 -0500 From: Austin Clements To: david@tethera.net Subject: Re: [PATCH 2/3] util: add xtalloc.[ch] Message-ID: <20121221020358.GR6187@mit.edu> References: <1355714648-23144-1-git-send-email-david@tethera.net> <1355714648-23144-3-git-send-email-david@tethera.net> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1355714648-23144-3-git-send-email-david@tethera.net> User-Agent: Mutt/1.5.21 (2010-09-15) X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFupmleLIzCtJLcpLzFFi42IRYrdT1514+HKAQcMGFYuNy34yW9xo7Wa0 uH5zJrMDs8evtrnMHs9W3WL22HLoPXMAcxSXTUpqTmZZapG+XQJXxoStW9kLFolXLLz3ha2B cY1QFyMnh4SAicSGpReZIWwxiQv31rOB2EIC+xglFr5k6mLkArI3MEpMujCTHcK5yCTRfe0M C4SzhFGi/UcDWDuLgKrEkb/HwdrZBDQktu1fzghiiwiISpzeNIkVxGYWsJdYNHsS0CQODmEB fYnPH+1ATF4BbYmFv7UgFldK7Pu/gR3E5hUQlDg58wkLRKeWxI1/IAdxANnSEsv/cYCEOQWc JKb/2gg2XFRARWLKyW1sExiFZiHpnoWkexZC9wJG5lWMsim5Vbq5iZk5xanJusXJiXl5qUW6 Jnq5mSV6qSmlmxhBYc4pyb+D8dtBpUOMAhyMSjy8ERaXA4RYE8uKK3MPMUpyMCmJ8urtBwrx JeWnVGYkFmfEF5XmpBYfYpTgYFYS4X2+BCjHm5JYWZValA+TkuZgURLnvZpy019IID2xJDU7 NbUgtQgmK8PBoSTBO+8QUKNgUWp6akVaZk4JQpqJgxNkOA/Q8OkgNbzFBYm5xZnpEPlTjIpS 4rztIAkBkERGaR5cLywNvWIUB3pFmFcdpIoHmMLgul8BDWYCGhykdwFkcEkiQkqqgTGYb73y oilLp00+tTHRKvuUcu6eRxrdjI+VP8+/URkTs+tGxK63XGlRR23Y5Np58rYWb+BOmyW6/CXL sWP+NocCDBd4ycyL5P94I+lAxilzAXHW7bM/TRdp+KBuVHgxMfJzRoP/8yXF+3v1khqunju3 f3a1pu2yNZ/b6l5bxqdYtD1dtkjH67USS3FGoqEWc1FxIgDsMh1wHgMAAA== Cc: notmuch@notmuchmail.org, David Bremner X-BeenThere: notmuch@notmuchmail.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 21 Dec 2012 02:04:03 -0000 Quoth david@tethera.net on Dec 16 at 11:24 pm: > From: David Bremner > > These are intended to be simple wrappers to provide slightly better > debugging information than what talloc currently provides natively. > --- > notmuch-client.h | 2 +- > util/Makefile.local | 2 +- > util/xtalloc.c | 15 +++++++++++++++ > util/xtalloc.h | 18 ++++++++++++++++++ > 4 files changed, 35 insertions(+), 2 deletions(-) > create mode 100644 util/xtalloc.c > create mode 100644 util/xtalloc.h > > diff --git a/notmuch-client.h b/notmuch-client.h > index d7b352e..60be030 100644 > --- a/notmuch-client.h > +++ b/notmuch-client.h > @@ -58,7 +58,7 @@ typedef GMimeCipherContext notmuch_crypto_context_t; > #include > #include > > -#include > +#include "xtalloc.h" > > #define unused(x) x __attribute__ ((unused)) > > diff --git a/util/Makefile.local b/util/Makefile.local > index a11e35b..8a62c00 100644 > --- a/util/Makefile.local > +++ b/util/Makefile.local > @@ -4,7 +4,7 @@ dir := util > extra_cflags += -I$(srcdir)/$(dir) > > libutil_c_srcs := $(dir)/xutil.c $(dir)/error_util.c $(dir)/hex-escape.c \ > - $(dir)/string-util.c > + $(dir)/string-util.c $(dir)/xtalloc.c > > libutil_modules := $(libutil_c_srcs:.c=.o) > > diff --git a/util/xtalloc.c b/util/xtalloc.c > new file mode 100644 > index 0000000..22834bd > --- /dev/null > +++ b/util/xtalloc.c > @@ -0,0 +1,15 @@ > +#include > +#include "xtalloc.h" > + > +char * > +xtalloc_strndup_named_const (void *ctx, const char *str, > + size_t len, const char *name) > +{ > + char *ptr = talloc_named_const (ctx, len + 1, name); > + > + if (ptr) { > + memcpy (ptr, str, len); This isn't safe. If the string at ptr is actually shorter than len, this may read past allocated memory and crash. Maybe this should just call talloc_strndup and talloc_set_name_const? > + *(ptr + len) = '\0'; > + } > + return ptr; > +} > diff --git a/util/xtalloc.h b/util/xtalloc.h > new file mode 100644 > index 0000000..3cc1179 > --- /dev/null > +++ b/util/xtalloc.h > @@ -0,0 +1,18 @@ > +#ifndef _XTALLOC_H > +#define _XTALLOC_H > + > +#include > + > +/* Like talloc_strndup, but take an extra parameter for the internal talloc > + * name (for debugging) */ > + > +char * > +xtalloc_strndup_named_const (void *ctx, const char *str, > + size_t len, const char *name); I agree with Tomi that these shouldn't be named with 'x'. For this one, it seems fine to simply drop the 'x', since the name is fully descriptive of what it does. > + > +/* use the __location__ macro from talloc.h to name a string according to its > + * source location */ > + > +#define xtalloc_strndup(ctx, str, len) xtalloc_strndup_named_const (ctx, str, len, __location__) For this, what about talloc_strndup_debug? > + > +#endif