Commit 0127d3c7 authored by Nicolas Dusart's avatar Nicolas Dusart Committed by Ludovic Rousseau

Simclist: avoid to divide by zero in list_findpos()

A division by zero with float is undefined in C.
For example gcc 6.3.0 reports (when detected):
dividebyzero.c:5:20: warning: division by zero [-Wdiv-by-zero]
  float a = (float)1/0;

But the code does NOT crash. The value of a is just infinity.

The problem is when Free Pascal runtime is used. In this case the code crashed.
parent 2f99e761
......@@ -449,7 +449,10 @@ static inline struct list_entry_s *list_findpos(const list_t *restrict l, int po
/* accept 1 slot overflow for fetching head and tail sentinels */
if (posstart < -1 || posstart > (int)l->numels) return NULL;
x = (float)(posstart+1) / l->numels;
if( l->numels != 0 )
x = (float)(posstart+1) / l->numels;
else
x = 1;
if (x <= 0.25) {
/* first quarter: get to posstart from head */
for (i = -1, ptr = l->head_sentinel; i < posstart; ptr = ptr->next, i++);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment