Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1syHlE-00FgDi-O0 for pgsql-hackers@arkaria.postgresql.org; Tue, 08 Oct 2024 21:28:52 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1syHlA-00Bpw6-Rq for pgsql-hackers@arkaria.postgresql.org; Tue, 08 Oct 2024 21:28:48 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1syHlA-00Bpvj-GB for pgsql-hackers@lists.postgresql.org; Tue, 08 Oct 2024 21:28:48 +0000 Received: from mail-qv1-xf29.google.com ([2607:f8b0:4864:20::f29]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1syHl3-003Chc-W0 for pgsql-hackers@postgresql.org; Tue, 08 Oct 2024 21:28:47 +0000 Received: by mail-qv1-xf29.google.com with SMTP id 6a1803df08f44-6cbc08e7495so8296796d6.3 for ; Tue, 08 Oct 2024 14:28:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728422921; x=1729027721; darn=postgresql.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=wRSZeva9euuWXDW76aDRO/Wbz33PzfyE4LGLl2HODe4=; b=FYIXic7n0BefzrjXjdG0XZw5JbyI/6AaXvu/9dXLYfrQ7yiT+pLGZN5QM5RVWUBF1W kK7vO5MDlrQxC7tYxzjwTBbMbVrnKn2R2vsy00KCN8cy9YEmstDNJUi59f1mZWrPdsnp byTQ8LcA4vHEbzQUOeWcQS26VmBT2d5uATSiO5P2S+yW2oz2tvrIEjjCbR41c4IImb5v pDDEmu7cYGnHBVjP7CRv5GTLd9+UdoAZmu6IUiqjmK9/cCOPR9eCLhM02iVbhXihnCid Z5n5LWsBOY9Y6cF7M7fUDsRV6AUg6iWWXDJ5abS20S4I/8TvvQGHSSEwRDwOhTD/PLSO Tm/w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728422921; x=1729027721; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=wRSZeva9euuWXDW76aDRO/Wbz33PzfyE4LGLl2HODe4=; b=BVegcs5bfYJxfx8VJcn5gZQ1yAyYxJtl6xHq95u4JBvUmWKqNk3PorGkw/r+eqoY12 A5cv55dN1m3cH9jMWrwP6Uo02CdXsUS3sku8QkDNf2w9/kIhZPH5Vgi8v674u7jDpwa6 gIXkHWckiz5Nl5wtTLbWKWEWCUKRN4g1N4c0guohAGG/GeDHbR0+VRXuXN3TGBnqfnTv KiBWSpNKjQsZp3ZRbSYZlNkT6T+OgCExAk9zt9tGF8eOuRyO0XXWwANXzd1CiHwQ2HZb MxOcqZOwMixq+chGjPi5LID3O3gKGRxF6ZSUEmcgqgGSNo9REJTZamDt/bMlcwVvOzt6 Cgiw== X-Gm-Message-State: AOJu0YxI1s4YhtNST5kFVjbi/MY7x5q+k5z/emN0cy9EIcgo6n4PPWw/ ddIpRAA9IKmlTnfBwV+RapE587CIX7zzhq8SLrCm2K5eDwvpIHz6 X-Google-Smtp-Source: AGHT+IFCdv99W0LVVK0jGFM31BSaxLYDm4Mr9ERKUNqCxpoSdlHbQ0QFSzFQkgDdwHRqyNvxLDh+mQ== X-Received: by 2002:a05:6214:4343:b0:6c5:ab6c:8eb1 with SMTP id 6a1803df08f44-6cbc953ad27mr8264986d6.16.1728422920952; Tue, 08 Oct 2024 14:28:40 -0700 (PDT) Received: from nathan (162-195-168-172.lightspeed.stlsmo.sbcglobal.net. [162.195.168.172]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-45da8cd7e27sm38551301cf.24.2024.10.08.14.28.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Oct 2024 14:28:40 -0700 (PDT) Date: Tue, 8 Oct 2024 16:28:37 -0500 From: Nathan Bossart To: Ranier Vilela Cc: Pg Hackers Subject: Re: Avoid possible overflow (src/port/bsearch_arg.c) Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Tue, Oct 08, 2024 at 04:09:00PM -0300, Ranier Vilela wrote: > The port function *bsearch_arg* mimics the C function > *bsearch*. > > The API signature is: > void * > bsearch_arg(const void *key, const void *base0, > size_t nmemb, size_t size, > int (*compar) (const void *, const void *, void *), > void *arg) > > So, the parameter *nmemb* is size_t. > Therefore, a call with nmemb greater than INT_MAX is possible. > > Internally the code uses the *int* type to iterate through the number of > members, which makes overflow possible. I traced this back to commit bfa2cee (v14), which both moved bsearch_arg() to its current location and adjusted the style a bit. Your patch looks reasonable to me. -- nathan