Received: by 2002:a25:31c3:0:0:0:0:0 with SMTP id x186csp971665ybx; Wed, 30 Oct 2019 08:01:46 -0700 (PDT) X-Google-Smtp-Source: APXvYqzBXp/Q/UnPHp7olGMogBEn/LXHcMUhAnbM8eRqw2RSzudyqO5TPxuXRiRCJz5RctcwZPz9 X-Received: by 2002:a17:907:1118:: with SMTP id qu24mr9396310ejb.235.1572447706207; Wed, 30 Oct 2019 08:01:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1572447706; cv=none; d=google.com; s=arc-20160816; b=cF0hHHhJ2775jDGf4Ly9DbFUygnHTfdDg/z43u/E+/zfaSG6pfPy1cWl/JFWy8LYSq NyHtfjFk4szPQPL5/hJBjynNjFJaSA1wD7o7UcRnezLtaCmQwjG5iS73j59xXrA4BntD 37gWDdQVdZtirMoDUWk10HrUyuGO1rZULYXsc9MhOULCixB4+F7GPbMsHSnaZ4x8oRCe X+bDyrSspy4LGf6aBROFh/VwQFgQ5w7Q++ZYoKPf+hvRooHPlzrJCBQInFyyceFwd+P3 MQo6KAJDdOXQ/EH/QDl185WZ8MjIKln1yKEb2EEgVpVGJSI41VymfpTFQf4SajkFHbA8 fA1w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:to:subject:dkim-signature; bh=t++HF3tscy3V/wh/kTRJzh1qufAuUyRQnA+rQJgsrZM=; b=DGHp95y6Xw+POvmDcDIxq2pv+LlluEWGdLaqeYT64VCa6wSAoFeYfmNa0kcKhAxM7x 4vb4LwRCXFfAyk+mZNw8poUGevU6RW3kJi1m+ecO3ZFeNQFBACvZjyU5yJbkTAlRuwvv WaB37XzhPc38pwT6swj0CHPDVPPb+eWqQkGoetrA23OV4uLeakbZ7W3zCD2Enm7x0PAm A21J1vsI18+c87wRMEpWXqJ9B+QFGwInkE2fKP79y8w9RfBTkyZ02L5KreC4/sr5c43/ kMKeiAxNJ0m1UukIXUD9Fs0XtqFxP78zcls6dqMoynaUcg1rQk6QqBXa+g1uGZb03t/P bBpg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rasmusvillemoes.dk header.s=google header.b=APStmm2+; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id n11si1338464ejb.240.2019.10.30.08.01.20; Wed, 30 Oct 2019 08:01:46 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@rasmusvillemoes.dk header.s=google header.b=APStmm2+; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726359AbfJ3PAF (ORCPT + 99 others); Wed, 30 Oct 2019 11:00:05 -0400 Received: from mail-lj1-f195.google.com ([209.85.208.195]:34291 "EHLO mail-lj1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726175AbfJ3PAF (ORCPT ); Wed, 30 Oct 2019 11:00:05 -0400 Received: by mail-lj1-f195.google.com with SMTP id 139so3068070ljf.1 for ; Wed, 30 Oct 2019 08:00:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rasmusvillemoes.dk; s=google; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language:content-transfer-encoding; bh=t++HF3tscy3V/wh/kTRJzh1qufAuUyRQnA+rQJgsrZM=; b=APStmm2+15s8QErOHJBekI63+aPACEuwIcUFXIWFGEuD/Njlb8KP75fiK7wNwkCEXz btNIjqZMF77IRMVrUfhaHPLc8imZyizKk+fu6rUZxOeSQNA4QYrndmfJQM1cFMmck3WP aG9Y6Lx1Cna4+EQOaaCE8ZcrbO1PcaqOUT6TE= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=t++HF3tscy3V/wh/kTRJzh1qufAuUyRQnA+rQJgsrZM=; b=dTG3UK5VX92dILBkyjbBb3OhS7aButcQEACeUZ+cYBgl9QC0sYCm/8i45Wlt5smwIb 8vVhM0wlLAu3rr5Yx3JI6MVTzOKkSpuW9nYHryvFQAn6vZRvM7DAcrMClP2LUDadNLRM v75V4C32OB88FhfQ8x2rxCRBSnNS2vt95Klwrev8y1G6LTck9dk+ithYbZjSaiO1RNWh 4ubPyN6xGNkphmYrNTIjj2ihQ5DR8vGby+y7C1dXWk8PLB9VWb3g/vBmnfFakeu59J3p WIOjwreFnU9CtBdxN6/wR8l7toaljhcYTzBU+fP7qDWQsrzw5AxwIPzutqPhM0ZsPFKi p63Q== X-Gm-Message-State: APjAAAVmR8CZXqpOLWqS/brS8Va4rpA3aI+UCvSzCSxMCxMaAzRSbOa5 kNUGsQewbGBZ9tGsm4NcHoY4PLRzoTTKzYYh X-Received: by 2002:a2e:99cf:: with SMTP id l15mr114618ljj.109.1572447601552; Wed, 30 Oct 2019 08:00:01 -0700 (PDT) Received: from [172.16.11.28] ([81.216.59.226]) by smtp.gmail.com with ESMTPSA id r5sm112435lfc.85.2019.10.30.07.59.59 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 30 Oct 2019 08:00:00 -0700 (PDT) Subject: Re: [PATCH] lib: string: reduce unnecessary loop in strncpy To: Liu Xiang , linux-kernel@vger.kernel.org References: <1572444859-3687-1-git-send-email-liuxiang_1999@126.com> From: Rasmus Villemoes Message-ID: Date: Wed, 30 Oct 2019 15:59:58 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.9.0 MIME-Version: 1.0 In-Reply-To: <1572444859-3687-1-git-send-email-liuxiang_1999@126.com> Content-Type: text/plain; charset=windows-1252 Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 30/10/2019 15.14, Liu Xiang wrote: > Now in strncpy, even src[0] is 0, loop will execute count times until > count is 0. It is better to exit the loop immediately when *src is 0. Please read "man strncpy". There's a reason the loop is written in that somewhat convoluted way: The behavior of strncpy is mandated by the C standard, and if the src string is shorter than the destination buffer, the rest must be 0-filled. So if we hit a nul byte before running out of count, we keep copying that nul byte to the rest of the destination. Rasmus