@@ -237,8 +237,8 @@ typedef struct AsyncQueueControl
237237 QueuePosition tail ; /* the global tail is equivalent to the tail
238238 * of the "slowest" backend */
239239 TimestampTz lastQueueFillWarn ; /* time of last queue-full msg */
240- QueueBackendStatus backend [1 ]; /* actually of length MaxBackends+1 */
241- /* DO NOT ADD FURTHER STRUCT MEMBERS HERE */
240+ QueueBackendStatus backend [FLEXIBLE_ARRAY_MEMBER ];
241+ /* backend[0] is not used; used entries are from [1] to [MaxBackends] */
242242} AsyncQueueControl ;
243243
244244static AsyncQueueControl * asyncQueueControl ;
@@ -303,7 +303,7 @@ typedef enum
303303typedef struct
304304{
305305 ListenActionKind action ;
306- char channel [1 ]; /* actually, as long as needed */
306+ char channel [FLEXIBLE_ARRAY_MEMBER ]; /* nul-terminated string */
307307} ListenAction ;
308308
309309static List * pendingActions = NIL ; /* list of ListenAction */
@@ -417,8 +417,8 @@ AsyncShmemSize(void)
417417 Size size ;
418418
419419 /* This had better match AsyncShmemInit */
420- size = mul_size (MaxBackends , sizeof (QueueBackendStatus ));
421- size = add_size (size , sizeof (AsyncQueueControl ));
420+ size = mul_size (MaxBackends + 1 , sizeof (QueueBackendStatus ));
421+ size = add_size (size , offsetof (AsyncQueueControl , backend ));
422422
423423 size = add_size (size , SimpleLruShmemSize (NUM_ASYNC_BUFFERS , 0 ));
424424
@@ -438,12 +438,11 @@ AsyncShmemInit(void)
438438 /*
439439 * Create or attach to the AsyncQueueControl structure.
440440 *
441- * The used entries in the backend[] array run from 1 to MaxBackends.
442- * sizeof(AsyncQueueControl) already includes space for the unused zero'th
443- * entry, but we need to add on space for the used entries.
441+ * The used entries in the backend[] array run from 1 to MaxBackends; the
442+ * zero'th entry is unused but must be allocated.
444443 */
445- size = mul_size (MaxBackends , sizeof (QueueBackendStatus ));
446- size = add_size (size , sizeof (AsyncQueueControl ));
444+ size = mul_size (MaxBackends + 1 , sizeof (QueueBackendStatus ));
445+ size = add_size (size , offsetof (AsyncQueueControl , backend ));
447446
448447 asyncQueueControl = (AsyncQueueControl * )
449448 ShmemInitStruct ("Async Queue Control" , size , & found );
@@ -605,7 +604,8 @@ queue_listen(ListenActionKind action, const char *channel)
605604 oldcontext = MemoryContextSwitchTo (CurTransactionContext );
606605
607606 /* space for terminating null is included in sizeof(ListenAction) */
608- actrec = (ListenAction * ) palloc (sizeof (ListenAction ) + strlen (channel ));
607+ actrec = (ListenAction * ) palloc (offsetof(ListenAction , channel ) +
608+ strlen (channel ) + 1 );
609609 actrec -> action = action ;
610610 strcpy (actrec -> channel , channel );
611611
0 commit comments