From owner-freebsd-net@FreeBSD.ORG Thu Nov 2 08:26:35 2006 Return-Path: X-Original-To: freebsd-net@freebsd.org Delivered-To: freebsd-net@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id B17BF16A417 for ; Thu, 2 Nov 2006 08:26:35 +0000 (UTC) (envelope-from antinvidia@gmail.com) Received: from nf-out-0910.google.com (nf-out-0910.google.com [64.233.182.190]) by mx1.FreeBSD.org (Postfix) with ESMTP id 9689343D83 for ; Thu, 2 Nov 2006 08:26:28 +0000 (GMT) (envelope-from antinvidia@gmail.com) Received: by nf-out-0910.google.com with SMTP id p77so933720nfc for ; Thu, 02 Nov 2006 00:26:27 -0800 (PST) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:to:subject:mime-version:content-type; b=o0X4kK44V43cg0VIveVE4FoAk/1VhYwlfsGQ6iltKIZtUhEcELpfi3mCTmRzec272PhtfM6WwOl6OHKj/pluqOslUdijmkLfWvG5rZ45tyMalid2VH5SK4MSwtVvNPah945whr5v3jjIZsPS6nxxDTBdQAvW5GdtO1Veq8mPJVs= Received: by 10.49.75.2 with SMTP id c2mr5219844nfl.1162455987221; Thu, 02 Nov 2006 00:26:27 -0800 (PST) Received: by 10.49.37.15 with HTTP; Thu, 2 Nov 2006 00:26:27 -0800 (PST) Message-ID: Date: Thu, 2 Nov 2006 08:26:27 +0000 From: . To: freebsd-net@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline X-Content-Filtered-By: Mailman/MimeDel 2.1.5 Subject: Reentrant problem with inet_ntoa in the kernel X-BeenThere: freebsd-net@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Networking and TCP/IP with FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 02 Nov 2006 08:26:35 -0000 Hi, I am confused by the use of inet_ntoa function in the kernel. The function inet_ntoa in the /sys/libkern/inet_ntoa.c uses a static array static char buf[4 * sizeof "123"]; to store the result. And it returns the address of the array to the caller. I think this inet_ntoa is not reentrant, though there are several functions calling it. If two functions call it simultaneously, the result will be corrupted. Though I haven't really encountered this situation, it may occur someday, especially when using multi-processors. There is another reentrant version of inet_ntoa called inet_ntoa_r in the same file. It has been there for several years, but just used by ipfw2 for about four times in 7-CURRENT. In my patch, I replaced all the calls to inet_ntoa with calls to inet_ntoa_r. By the way, some of the original calls is written in this style: strcpy(buf, inet_ntoa(ip)) The modified code is written in this style inet_ntoa_r(ip, buf) This change avoids a call to strcpy, and can save a little time. Here is the patch. http://people.freebsd.org/~delphij/misc/patch-itoa-by-nodummy-at-yeah-net I've already sent to PR(kern/104738), but got no reply, maybe it should be discussed here first? Thanks MQ