Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue #27 (UB/Flow-cast overflow) and similar ones #28

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 96 additions & 16 deletions libspeex/fixed_debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,13 @@ static inline short NEG16(int x)
}
res = -x;
if (!VERIFY_SHORT(res))
fprintf (stderr, "NEG16: output is not short: %d\n", (int)res);
{
fprintf (stderr, "NEG16: output is not short: %d\n", (int)res);
if (res > 32767)
res = 32767;
if (res < -32768)
res = -32768;
}
spx_mips++;
return res;
}
Expand Down Expand Up @@ -110,7 +116,13 @@ static inline short _SHR16(int a, int shift, char *file, int line)
}
res = a>>shift;
if (!VERIFY_SHORT(res))
fprintf (stderr, "SHR16: output is not short: %d in %s: line %d\n", res, file, line);
{
fprintf (stderr, "SHR16: output is not short: %d in %s: line %d\n", res, file, line);
if (res > 32767)
res = 32767;
if (res < -32768)
res = -32768;
}
spx_mips++;
return res;
}
Expand All @@ -124,7 +136,13 @@ static inline short _SHL16(int a, int shift, char *file, int line)
}
res = (int)((unsigned)a<<shift);
if (!VERIFY_SHORT(res))
fprintf (stderr, "SHL16: output is not short: %d in %s: line %d\n", res, file, line);
{
fprintf (stderr, "SHL16: output is not short: %d in %s: line %d\n", res, file, line);
if (res > 32767)
res = 32767;
if (res < -32768)
res = -32768;
}
spx_mips++;
return res;
}
Expand Down Expand Up @@ -181,7 +199,11 @@ static inline short _ADD16(int a, int b, char *file, int line)
res = a+b;
if (!VERIFY_SHORT(res))
{
fprintf (stderr, "ADD16: output is not short: %d+%d=%d in %s: line %d\n", a,b,res, file, line);
fprintf (stderr, "ADD16: output is not short: %d+%d=%d in %s: line %d\n", a,b,res, file, line);
if (res > 32767)
res = 32767;
if (res < -32768)
res = -32768;
}
spx_mips++;
return res;
Expand All @@ -197,7 +219,13 @@ static inline short _SUB16(int a, int b, char *file, int line)
}
res = a-b;
if (!VERIFY_SHORT(res))
fprintf (stderr, "SUB16: output is not short: %d in %s: line %d\n", res, file, line);
{
fprintf (stderr, "SUB16: output is not short: %d in %s: line %d\n", res, file, line);
if (res > 32767)
res = 32767;
if (res < -32768)
res = -32768;
}
spx_mips++;
return res;
}
Expand Down Expand Up @@ -227,8 +255,14 @@ static inline int SUB32(long long a, long long b)
fprintf (stderr, "SUB32: inputs are not int: %d %d\n", (int)a, (int)b);
}
res = a-b;
if (!VERIFY_INT(res))
fprintf (stderr, "SUB32: output is not int: %d\n", (int)res);
if (!VERIFY_SHORT(res))
{
fprintf (stderr, "SUB32: output is not int: %d\n", (int)res);
if (res > 32767)
res = 32767;
if (res < -32768)
res = -32768;
}
spx_mips++;
return res;
}
Expand All @@ -245,7 +279,13 @@ static inline short MULT16_16_16(int a, int b)
}
res = a*b;
if (!VERIFY_SHORT(res))
fprintf (stderr, "MULT16_16_16: output is not short: %d\n", res);
{
fprintf (stderr, "MULT16_16_16: output is not short: %d\n", res);
if (res > 32767)
res = 32767;
if (res < -32768)
res = -32768;
}
spx_mips++;
return res;
}
Expand Down Expand Up @@ -329,8 +369,14 @@ static inline int MULT16_16_Q11_32(int a, int b)
}
res = ((long long)a)*b;
res >>= 11;
if (!VERIFY_INT(res))
fprintf (stderr, "MULT16_16_Q11: output is not short: %d*%d=%d\n", (int)a, (int)b, (int)res);
if (!VERIFY_SHORT(res))
{
fprintf (stderr, "MULT16_16_Q11: output is not short: %d*%d=%d\n", (int)a, (int)b, (int)res);
if (res > 32767)
res = 32767;
if (res < -32768)
res = -32768;
}
spx_mips+=3;
return res;
}
Expand All @@ -344,7 +390,13 @@ static inline short MULT16_16_Q13(int a, int b)
res = ((long long)a)*b;
res >>= 13;
if (!VERIFY_SHORT(res))
fprintf (stderr, "MULT16_16_Q13: output is not short: %d*%d=%d\n", a, b, (int)res);
{
fprintf (stderr, "MULT16_16_Q13: output is not short: %d*%d=%d\n", a, b, (int)res);
if (res > 32767)
res = 32767;
if (res < -32768)
res = -32768;
}
spx_mips+=3;
return res;
}
Expand All @@ -358,7 +410,13 @@ static inline short MULT16_16_Q14(int a, int b)
res = ((long long)a)*b;
res >>= 14;
if (!VERIFY_SHORT(res))
fprintf (stderr, "MULT16_16_Q14: output is not short: %d\n", (int)res);
{
fprintf (stderr, "MULT16_16_Q14: output is not short: %d\n", (int)res);
if (res > 32767)
res = 32767;
if (res < -32768)
res = -32768;
}
spx_mips+=3;
return res;
}
Expand All @@ -373,7 +431,11 @@ static inline short MULT16_16_Q15(int a, int b)
res >>= 15;
if (!VERIFY_SHORT(res))
{
fprintf (stderr, "MULT16_16_Q15: output is not short: %d\n", (int)res);
fprintf(stderr, "MULT16_16_P14: output is not short: %d*%d=%d\n", a, b, (int)res);
if (res > 32767)
res = 32767;
if (res < -32768)
res = -32768;
}
spx_mips+=3;
return res;
Expand All @@ -392,7 +454,13 @@ static inline short MULT16_16_P13(int a, int b)
fprintf (stderr, "MULT16_16_P13: overflow: %d*%d=%d\n", a, b, (int)res);
res >>= 13;
if (!VERIFY_SHORT(res))
fprintf (stderr, "MULT16_16_P13: output is not short: %d*%d=%d\n", a, b, (int)res);
{
fprintf (stderr, "MULT16_16_P13: output is not short: %d*%d=%d\n", a, b, (int)res);
if (res > 32767)
res = 32767;
if (res < -32768)
res = -32768;
}
spx_mips+=4;
return res;
}
Expand All @@ -409,7 +477,13 @@ static inline short MULT16_16_P14(int a, int b)
fprintf (stderr, "MULT16_16_P14: overflow: %d*%d=%d\n", a, b, (int)res);
res >>= 14;
if (!VERIFY_SHORT(res))
fprintf (stderr, "MULT16_16_P14: output is not short: %d*%d=%d\n", a, b, (int)res);
{
fprintf(stderr, "MULT16_16_P14: output is not short: %d*%d=%d\n", a, b, (int)res);
if (res > 32767)
res = 32767;
if (res < -32768)
res = -32768;
}
spx_mips+=4;
return res;
}
Expand All @@ -426,7 +500,13 @@ static inline short MULT16_16_P15(int a, int b)
fprintf (stderr, "MULT16_16_P15: overflow: %d*%d=%d\n", a, b, (int)res);
res >>= 15;
if (!VERIFY_SHORT(res))
fprintf (stderr, "MULT16_16_P15: output is not short: %d*%d=%d\n", a, b, (int)res);
{
fprintf(stderr, "MULT16_16_P14: output is not short: %d*%d=%d\n", a, b, (int)res);
if (res > 32767)
res = 32767;
if (res < -32768)
res = -32768;
}
spx_mips+=4;
return res;
}
Expand Down