From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-oa1-x32.google.com (mail-oa1-x32.google.com [IPv6:2001:4860:4864:20::32]) by sourceware.org (Postfix) with ESMTPS id 78BEC384F01E for ; Thu, 2 Jun 2022 18:55:59 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 78BEC384F01E Received: by mail-oa1-x32.google.com with SMTP id 586e51a60fabf-e656032735so7949875fac.0 for ; Thu, 02 Jun 2022 11:55:59 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:cc:references:from:in-reply-to :content-transfer-encoding; bh=+sCm7rG1Z3LzWtQOxBSDPFidquy+ic4w8OCSboALPdg=; b=s452X+NruVAcDun04cNmgcfXnK1oAeT5V0SsDr4OW3kgv3OL73nGzKYwEGPswzn6FU itRqSA2ebEpNObvUpZZMwyMeZQX4u2Wq6HGiaVg54EzRoJ0TRKADyGrMeuGhUid2x6lv WKaaT2xV23ns2dMm6WGYBKeazeWOE/3ZRFzSaSiWSxYGKqTdT0GTA0bQHCaCpZokL++1 sQXRWWvdt6VN2MO3QD8A4KNPJALR80epPfxY7T9Q/McPrmE7x+NQT/aYyAjGf0mpHHW2 Dyl4ZRX1A57Pn7HKEl1JLcwTQA8rxglnmQiMw221OuZDXt3nXgsxi6uVy5KzFKeoVT0r kAvA== X-Gm-Message-State: AOAM533vSNZTii+hJx7V9clPfVW5ROuTX3x3JtLtSbbfoqTGn4F9/Fkq srYDBroUqLcuvevJ/C06QWStbXgAZ3ZLQg== X-Google-Smtp-Source: ABdhPJyyiBWAOcWcIVF6f7wubAqw5tPHxzPXBNtm6vZqK/YobJLfRSbKnuu1oUPbCxCGctjkHnnHfQ== X-Received: by 2002:a05:6870:7390:b0:f3:2f65:23cd with SMTP id z16-20020a056870739000b000f32f6523cdmr13078929oam.201.1654196158794; Thu, 02 Jun 2022 11:55:58 -0700 (PDT) Received: from ?IPV6:2804:431:c7ca:e39c:d866:26cd:928:370a? ([2804:431:c7ca:e39c:d866:26cd:928:370a]) by smtp.gmail.com with ESMTPSA id m19-20020a056820051300b0035eb4e5a6b0sm2996210ooj.6.2022.06.02.11.55.57 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 02 Jun 2022 11:55:58 -0700 (PDT) Message-ID: Date: Thu, 2 Jun 2022 15:55:55 -0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.10.0 Subject: Re: [PATCH v2 05/14] stdio-common: Introduce buffers for implementing printf Content-Language: en-US To: Florian Weimer Cc: libc-alpha@sourceware.org References: <93f2e269-daf5-2845-618e-1f49784be6d3@linaro.org> <87mtevvtet.fsf@oldenburg.str.redhat.com> <0d1bf515-67bb-91e1-89bb-9c55abad9de6@linaro.org> <87pmjqsyb5.fsf@oldenburg.str.redhat.com> From: Adhemerval Zanella In-Reply-To: <87pmjqsyb5.fsf@oldenburg.str.redhat.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-7.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, NICE_REPLY_A, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 02 Jun 2022 18:56:01 -0000 On 02/06/2022 15:41, Florian Weimer wrote: > * Adhemerval Zanella: > >>>>> +void >>>>> +Xprintf (buffer_puts_1) (struct Xprintf_buffer *buf, const CHAR_T *s) >>>>> +{ >>>>> + if (__glibc_unlikely (Xprintf_buffer_has_failed (buf))) >>>>> + return; >>>>> + >>>>> + while (*s != 0) >>>>> + { >>>>> + if (buf->write_ptr == buf->write_end && !Xprintf_buffer_flush (buf)) >>>>> + return; >>>>> + assert (buf->write_ptr != buf->write_end); >>>>> + size_t to_copy = STRNLEN (s, buf->write_end - buf->write_ptr); >>>>> + buf->write_ptr = MEMPCPY (buf->write_ptr, s, to_copy); >>>>> + s += to_copy; >>>>> + } >>>>> +} >>>> >>>> Does it really need a loop here? >>> >>> Yes, we might reach the end of the buffer with data left to write, so we >>> have to flush it. This can happen with asprintf and of course in the >>> FILE * case. >>> >> >> But in this case case you will just return right? I am trying to understand >> where the loop will be executed twice in this case. > > No, the return happens only if we need to flush (==, no room in the > buffer) and the flush fails, e.g. due to a write system call failure, or > maybe because we have reached the end of the buffer for snprintf. > Sigh, I forgot about the stage buffer you keep for printf_buffer_to_file.