From 58ecb1ac15cadec832fc5e539c250b0a6b1a0b90 Mon Sep 17 00:00:00 2001
From: Gregory Maxwell <greg@xiph.org>
Date: Mon, 9 May 2011 13:16:30 -0400
Subject: [PATCH] The encoder would crash in the PVQ search if fed NaNs via the
 float interface. This patch protects against it in two sufficient ways:
 Making the PVQ search robust against NaNs and by squashing NaNs to zero on
 input.

Thanks to David Richards for reporting this failure mode.
---
 libcelt/celt.c | 2 ++
 libcelt/vq.c   | 4 +++-
 2 files changed, 5 insertions(+), 1 deletion(-)

diff --git a/libcelt/celt.c b/libcelt/celt.c
index 671ac58a2..63fa3d874 100644
--- a/libcelt/celt.c
+++ b/libcelt/celt.c
@@ -1086,6 +1086,8 @@ int celt_encode_with_ec_float(CELTEncoder * restrict st, const celt_sig * pcm, i
 
             x = SCALEIN(*pcmp);
 #ifndef FIXED_POINT
+            if (!(x==x))
+               x = 0;
             if (st->clip)
                x = MAX32(-65536.f, MIN32(65536.f,x));
 #endif
diff --git a/libcelt/vq.c b/libcelt/vq.c
index 22aa03bd6..5c743511c 100644
--- a/libcelt/vq.c
+++ b/libcelt/vq.c
@@ -223,7 +223,9 @@ unsigned alg_quant(celt_norm *X, int N, int K, int spread, int B,
 #ifdef FIXED_POINT
       if (sum <= K)
 #else
-      if (sum <= EPSILON)
+      /* Prevents infinities and NaNs from causing too many pulses
+         to be allocated. 64 is an approximation of infinity here. */
+      if (!(sum > EPSILON && sum < 64))
 #endif
       {
          X[0] = QCONST16(1.f,14);
-- 
GitLab