在 simclist 库中除以零

divide by zero in simclist library

在 simclist link 列表库中,我 运行 遇到函数 list_findpos() 中出现被零除的问题。通常我会犹豫是否要修改一个广泛使用的第 3 方库,并且它必须有充分的理由来做它所做的事情(我没有排除库被错误使用)。函数定义为:

/* set tmp to point to element at index posstart in l */
static inline struct list_entry_s *list_findpos(const list_t *restrict l, int posstart) {
    struct list_entry_s *ptr;
    float x;
    int i;

    /* 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 (x <= 0.25) {
        /* first quarter: get to posstart from head */
        for (i = -1, ptr = l->head_sentinel; i < posstart; ptr = ptr->next, i++);
    } else if (x < 0.5) {
        /* second quarter: get to posstart from mid */
        for (i = (l->numels-1)/2, ptr = l->mid; i > posstart; ptr = ptr->prev, i--);
    } else if (x <= 0.75) {
        /* third quarter: get to posstart from mid */
        for (i = (l->numels-1)/2, ptr = l->mid; i < posstart; ptr = ptr->next, i++);
    } else {
        /* fourth quarter: get to posstart from tail */
    for (i = l->numels, ptr = l->tail_sentinel; i > posstart; ptr = ptr->prev, i--);
    }

    return ptr;
}

如果l->numels0,那么第9行的表达式就有问题了。其他代码 运行 没问题(没有泄漏或段错误),但英特尔检查员说这一行发生了被零除。提议的更改是:

/* set tmp to point to element at index posstart in l */
static inline struct list_entry_s *list_findpos(const list_t *restrict l, int posstart) {
    struct list_entry_s *ptr;
    float x;
    int i;

    /* accept 1 slot overflow for fetching head and tail sentinels */
    if (posstart < -1 || posstart > (int)l->numels) return NULL;

    if (l->numels == 0){ //avoid division-by-zero on first element
       ptr = l->head_sentinel;
    }
    else{
       x = (float)(posstart + 1) / l->numels;
       if (x <= 0.25) {
          /* first quarter: get to posstart from head */
          for (i = -1, ptr = l->head_sentinel; i < posstart; ptr = ptr->next, i++);
       }
       else if (x < 0.5) {
          /* second quarter: get to posstart from mid */
          for (i = (l->numels - 1) / 2, ptr = l->mid; i > posstart; ptr = ptr->prev, i--);
       }
       else if (x <= 0.75) {
          /* third quarter: get to posstart from mid */
          for (i = (l->numels - 1) / 2, ptr = l->mid; i < posstart; ptr = ptr->next, i++);
       }
       else {
          /* fourth quarter: get to posstart from tail */
          for (i = l->numels, ptr = l->tail_sentinel; i > posstart; ptr = ptr->prev, i--);
       }
    }
    return ptr;
}

想知道是否有人可以对此发表评论,是否有使用 simclist 的经验?


编辑

list_findpos 不是 API 函数;相反,它是一个内部函数。导致问题的调用顺序是:

/* ... */
list_init(...) 
list_attributes_copy(...)
list_append(...) /* <---- list_findpos() is called here, divide by zero */

这个函数不是库的一部分 API;这完全是内部的。为了修复它,库的 author/maintainer 需要知道的是导致它失败的 API 调用序列。