]> git.ipfire.org Git - thirdparty/chrony.git/commitdiff
refclock: avoid reallocation of refclock instances
authorMiroslav Lichvar <mlichvar@redhat.com>
Wed, 11 May 2016 14:31:51 +0000 (16:31 +0200)
committerMiroslav Lichvar <mlichvar@redhat.com>
Thu, 12 May 2016 11:27:46 +0000 (13:27 +0200)
Change the array with refclock instances to store just pointers and
avoid reallocation of the instances. This fixes a bug with the SOCK
refclock, which uses the pointer to the instance in a file handler and
which was invalid when the instance was reallocated (after adding
another refclock).

The bug is from commit d92583ed330f4c1f5f29fc1fc7c01d2a19d12319.

refclock.c

index 59765f81764393df6f4fb933d2e66d92d89ed13a..7ee1468fa7ab07fe9686ae60872b305ac1d06e39 100644 (file)
@@ -87,7 +87,7 @@ struct RCL_Instance_Record {
   SRC_Instance source;
 };
 
-/* Array of RCL_Instance_Record */
+/* Array of pointers to RCL_Instance_Record */
 static ARR_Instance refclocks;
 
 static LOG_FileID logfileid;
@@ -114,13 +114,13 @@ static void filter_add_dispersion(struct MedianFilter *filter, double dispersion
 static RCL_Instance
 get_refclock(unsigned int index)
 {
-  return (RCL_Instance)ARR_GetElement(refclocks, index);
+  return *(RCL_Instance *)ARR_GetElement(refclocks, index);
 }
 
 void
 RCL_Initialise(void)
 {
-  refclocks = ARR_CreateInstance(sizeof (struct RCL_Instance_Record));
+  refclocks = ARR_CreateInstance(sizeof (RCL_Instance));
 
   CNF_AddRefclocks();
 
@@ -148,6 +148,7 @@ RCL_Finalise(void)
     filter_fini(&inst->filter);
     Free(inst->driver_parameter);
     SRC_DestroyInstance(inst->source);
+    Free(inst);
   }
 
   if (ARR_GetSize(refclocks) > 0) {
@@ -162,8 +163,10 @@ int
 RCL_AddRefclock(RefclockParameters *params)
 {
   int pps_source = 0;
+  RCL_Instance inst;
 
-  RCL_Instance inst = ARR_GetNewElement(refclocks);
+  inst = MallocNew(struct RCL_Instance_Record);
+  *(RCL_Instance *)ARR_GetNewElement(refclocks) = inst;
 
   if (strcmp(params->driver_name, "SHM") == 0) {
     inst->driver = &RCL_SHM_driver;