From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS22989 209.51.188.0/24 X-Spam-Status: No, score=-3.7 required=3.0 tests=AWL,BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_PASS shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dcvr.yhbt.net (Postfix) with ESMTPS id A821E1F463 for ; Tue, 31 Dec 2019 09:54:13 +0000 (UTC) Received: from localhost ([::1]:40708 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1imEE8-0005bl-6D for normalperson@yhbt.net; Tue, 31 Dec 2019 04:54:12 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:54901) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1imEE2-0005ba-FL for bug-gnulib@gnu.org; Tue, 31 Dec 2019 04:54:08 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1imEE1-0000xV-6a for bug-gnulib@gnu.org; Tue, 31 Dec 2019 04:54:06 -0500 Received: from mo6-p00-ob.smtp.rzone.de ([2a01:238:20a:202:5300::12]:15335) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1imEE0-0000ec-Eb for bug-gnulib@gnu.org; Tue, 31 Dec 2019 04:54:05 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; t=1577786040; s=strato-dkim-0002; d=clisp.org; h=References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: X-RZG-CLASS-ID:X-RZG-AUTH:From:Subject:Sender; bh=8gl1xlhCYiBdVQ0DhuOYHZ6MIAa4aSHr0yev5B31Tvg=; b=SYMxPcPZkZPt61xQ0dQmpfxYEePLr5aAGZwJcnctddTiQ8rwRSy565qrNuIvq+cfD+ MOKkDIY6c5oMCzwKbRpg2QVnQWh6IVKpQoqCNWOqE6EUw7VVIEbY+llsR02DSh7BF67u Gud5EE8nG40wPYleazSz1v8hoVxzxd76KN9THHWfdHebrxOGput40NsAGsffmR53UKL+ GIgXDFCigsOK/E9QHWfFoIVbruMKX/veYk/n+lSE18LVgo0VcxLIftm6vt1OhhB098M7 xlKDYE6zoErDy8z4F139/ag9hPwl7cYrxtDztx0v9LDt9kOVESU+XJar0DSP6ylkeXbO TWWQ== X-RZG-AUTH: ":Ln4Re0+Ic/6oZXR1YgKryK8brlshOcZlIWs+iCP5vnk6shH+AHjwLuWOH6fzxfs=" X-RZG-CLASS-ID: mo00 Received: from bruno.haible.de by smtp.strato.de (RZmta 46.1.3 DYNA|AUTH) with ESMTPSA id R06a06vBV9s0EMO (using TLSv1 with cipher ECDHE-RSA-AES256-SHA (curve X9_62_prime256v1 with 256 ECDH bits, eq. 3072 bits RSA)) (Client did not present a certificate); Tue, 31 Dec 2019 10:54:00 +0100 (CET) From: Bruno Haible To: Tim =?ISO-8859-1?Q?R=FChsen?= Subject: Re: string types Date: Tue, 31 Dec 2019 10:53:59 +0100 Message-ID: <7775995.zBxFH0rR4k@omega> User-Agent: KMail/5.1.3 (Linux/4.4.0-170-generic; KDE/5.18.0; x86_64; ; ) In-Reply-To: <82e3e796-f32d-3318-56e8-15c9e2cc2866@gmx.de> References: <175192568.e2XXTFFdkW@omega> <2179574.G9OhZXe8sF@omega> <82e3e796-f32d-3318-56e8-15c9e2cc2866@gmx.de> MIME-Version: 1.0 Content-Transfer-Encoding: 7Bit Content-Type: text/plain; charset="us-ascii" X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a01:238:20a:202:5300::12 X-BeenThere: bug-gnulib@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Gnulib discussion list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: bug-gnulib@gnu.org, ag Errors-To: bug-gnulib-bounces+normalperson=yhbt.net@gnu.org Sender: "bug-gnulib" Hi Tim, > > - providing primitives for string allocation reduces the amount of buffer > > overflow bugs that otherwise occur in this area. [1] > > [1] https://lists.gnu.org/archive/html/bug-gnulib/2019-09/msg00031.html > here is a string concatenation function without ellipsis, analogue to > writev() and struct iovec - just a suggestion. Instead of 'struct > strvec' a new string_t type would be handy. > > #include > #include > #include > #include > > struct strvec { > char *strv_base; > size_t strv_len; > }; > > __attribute__ ((nonnull (1))) > char *concat_stringv(const struct strvec *strv) > { > const struct strvec *str; > size_t len = 0; > char *buf; > > for (str = strv; str->strv_base; str++) > len += str->strv_len; > > if (!(buf = malloc(len + 1))) > return buf; > > len = 0; > for (str = strv; str->strv_base; len += str->strv_len, str++) > memcpy(buf + len, str->strv_base, str->strv_len); > > buf[len] = 0; > > return buf; > } > > void main(void) > { > char *s = concat_stringv((struct strvec []) { > { "a", 1 }, > { "b", 1 }, > { NULL } > }); This looks good. It brings us one step closer to the stated goal [1]. Would you like to contribute such a 'string-alloc' module that, together with 'strdup' and 'asprintf', removes most needs to create a string's contents "by hand"? Regarding the type name: There can't be a 'string_t' in C, I would say, because you will always have the NUL-terminated strings on one side and what you call a 'wget_string' on the other side, and there can't be a clear winner between both. Bruno