Add UP_CAST macro, for safer casting from contained data to its container.
[pspp-builds.git] / src / math / percentiles.c
index 3aec0c96fdd01514676ef17a7222c959b789da0d..2bde9d6f979bb5c410557bf904e06c2522702317 100644 (file)
@@ -24,6 +24,7 @@
 #define N_(msgid) msgid
 
 #include <libpspp/assertion.h>
+#include <libpspp/cast.h>
 #include <data/val-type.h>
 #include <gl/xalloc.h>
 #include <data/variable.h>
@@ -154,9 +155,10 @@ percentile_calculate (const struct percentile *ptl, enum pc_alg alg)
 static void
 destroy (struct statistic *stat)
 {
-  struct order_stats *os = (struct order_stats *) stat;
+  struct percentile *ptl = UP_CAST (stat, struct percentile, parent.parent);
+  struct order_stats *os = &ptl->parent;
   free (os->k);
-  free (os);
+  free (ptl);
 }