Subject: Fix condition in shm_toc and remove unused function shm_toc_freespace.

From: Zhang Mingli <[email-masked]>
Date: Thu, 12 Jan 2023 14:34:01 +0800

Hi, hackers

Some conditions in shm_toc_insert and shm_toc_allocate are bogus, like:

	if (toc_bytes + nbytes > total_bytes || toc_bytes + nbytes < toc_bytes)

Remove the condition `toc_bytes + nbytes < toc_bytes` and take a sizeof(shm_entry) into account in shm_toc_allocate though
shm_toc_allocate does that too.

	/* Check for memory exhaustion and overflow. */
	- if (toc_bytes + nbytes > total_bytes || toc_bytes + nbytes < toc_bytes)
	+ if (toc_bytes + sizeof(shm_toc_entry) + nbytes > total_bytes)
	 {
 	SpinLockRelease(&toc->toc_mutex);

shm_toc_freespace is introduced with shm_toc by original commit 6ddd5137b2, but is not used since then, so remove it.

Regards,
Zhang Mingli

---

From: Zhang Mingli <[email-masked]>
Date: Thu, 12 Jan 2023 14:50:13 +0800

Hi,

> shm_toc_allocate does that too.
  shm_toc_insert does that too, and  we can report error earlier.

Regards,
Zhang Mingli

---

From: Richard Guo <[email-masked]>
Date: Thu, 12 Jan 2023 16:54:40 +0800

On Thu, Jan 12, 2023 at 2:50 PM Zhang Mingli <[email-masked]> wrote:

> Remove the condition `toc_bytes + nbytes < toc_bytes` and take a
> sizeof(shm_entry) into account in shm_toc_allocate though

I don't think we should consider sizeof(shm_toc_entry) in the 'if'
condition in shm_toc_allocate, as this function is not in charge of
allocating a new TOC entry.  That's what shm_toc_insert does.

Other parts of this patch look good to me.

Thanks
Richard

---

From: Zhang Mingli <[email-masked]>
Date: Fri, 13 Jan 2023 23:09:30 +0800

Hi,

Regards,
Zhang Mingli
On Jan 12, 2023, 16:54 +0800, Richard Guo <[email-masked]>, wrote:
> > > 		if (toc_bytes + nbytes > total_bytes || toc_bytes + nbytes < toc_bytes)Remove the condition `toc_bytes + nbytes < toc_bytes` and take a sizeof(shm_entry) into account in shm_toc_allocate though
Thanks for review.
Make sense.
Even reserve a sizeof(shm_toc_entry) when shm_toc_allocate, it cloud happen that there is no space  when shm_toc_insert
in case of other processes may take space after that.
Patch updated.