@@ -2760,27 +2760,33 @@ pgstat_get_backend_current_activity(int pid, bool checkUser)
2760
2760
* pgstat_get_crashed_backend_activity() -
2761
2761
*
2762
2762
* Return a string representing the current activity of the backend with
2763
- * the specified PID. Like the function above, but reads shared memory with
2764
- * the expectation that it may be corrupt. Returns either a pointer to a
2765
- * constant string, or writes into the 'buffer' argument and returns it.
2763
+ * the specified PID. Like the function above, but reads shared memory with
2764
+ * the expectation that it may be corrupt. On success, copy the string
2765
+ * into the "buffer" argument and return that pointer. On failure,
2766
+ * return NULL.
2766
2767
*
2767
- * This function is only intended to be used by postmaster to report the
2768
- * query that crashed the backend. In particular, no attempt is made to
2768
+ * This function is only intended to be used by the postmaster to report the
2769
+ * query that crashed a backend. In particular, no attempt is made to
2769
2770
* follow the correct concurrency protocol when accessing the
2770
- * BackendStatusArray. But that's OK, in the worst case we'll return a
2771
- * corrupted message. We also must take care not to trip on ereport(ERROR).
2772
- *
2773
- * Note: return strings for special cases match pg_stat_get_backend_activity.
2771
+ * BackendStatusArray. But that's OK, in the worst case we'll return a
2772
+ * corrupted message. We also must take care not to trip on ereport(ERROR).
2774
2773
* ----------
2775
2774
*/
2776
2775
const char *
2777
- pgstat_get_crashed_backend_activity (int pid , char * buffer ,
2778
- int len )
2776
+ pgstat_get_crashed_backend_activity (int pid , char * buffer , int buflen )
2779
2777
{
2780
2778
volatile PgBackendStatus * beentry ;
2781
2779
int i ;
2782
2780
2783
2781
beentry = BackendStatusArray ;
2782
+
2783
+ /*
2784
+ * We probably shouldn't get here before shared memory has been set up,
2785
+ * but be safe.
2786
+ */
2787
+ if (beentry == NULL || BackendActivityBuffer == NULL )
2788
+ return NULL ;
2789
+
2784
2790
for (i = 1 ; i <= MaxBackends ; i ++ )
2785
2791
{
2786
2792
if (beentry -> st_procpid == pid )
@@ -2790,26 +2796,29 @@ pgstat_get_crashed_backend_activity(int pid, char *buffer,
2790
2796
const char * activity_last ;
2791
2797
2792
2798
/*
2793
- * We can 't access activity pointer before we verify that it
2794
- * falls into BackendActivityBuffer. To make sure that the entire
2795
- * string including its ending is contained within the buffer,
2796
- * we subtract one activity length from it .
2799
+ * We mustn 't access activity string before we verify that it
2800
+ * falls within the BackendActivityBuffer. To make sure that the
2801
+ * entire string including its ending is contained within the
2802
+ * buffer, subtract one activity length from the buffer size .
2797
2803
*/
2798
2804
activity_last = BackendActivityBuffer + BackendActivityBufferSize
2799
- - pgstat_track_activity_query_size ;
2805
+ - pgstat_track_activity_query_size ;
2800
2806
2801
2807
if (activity < BackendActivityBuffer ||
2802
2808
activity > activity_last )
2803
- return "<command string corrupt>" ;
2809
+ return NULL ;
2804
2810
2805
- if (* (activity ) == '\0' )
2806
- return "<command string empty>" ;
2811
+ /* If no string available, no point in a report */
2812
+ if (activity [0 ] == '\0' )
2813
+ return NULL ;
2807
2814
2808
2815
/*
2809
2816
* Copy only ASCII-safe characters so we don't run into encoding
2810
- * problems when reporting the message.
2817
+ * problems when reporting the message; and be sure not to run
2818
+ * off the end of memory.
2811
2819
*/
2812
- ascii_safe_strncpy (buffer , activity , len );
2820
+ ascii_safe_strlcpy (buffer , activity ,
2821
+ Min (buflen , pgstat_track_activity_query_size ));
2813
2822
2814
2823
return buffer ;
2815
2824
}
@@ -2818,9 +2827,10 @@ pgstat_get_crashed_backend_activity(int pid, char *buffer,
2818
2827
}
2819
2828
2820
2829
/* PID not found */
2821
- return "<backend information not available>" ;
2830
+ return NULL ;
2822
2831
}
2823
2832
2833
+
2824
2834
/* ------------------------------------------------------------
2825
2835
* Local support functions follow
2826
2836
* ------------------------------------------------------------
0 commit comments