我正在尝试找到队列的 C 代码中的安全漏洞

发布于 01-02 21:54 字数 792 浏览 2 评论 0原文

我知道 malloc 部分有些可疑,但我很难看出其中的不安全之处:

//que structure
typedef struct queue{
         int *que;  // the actual array of queue elements
         int head;   // the head index in que of the queue
         int count;  /// number of elements in queue
         int size; // max number of elements in queue
    } QUEUE;


void qManage(QUEUE **qptr, int flag, int size){
      if(flag){
              /* allocate a new queue */
              *qptr = malloc(sizeof(QUEUE));
              (*qptr)->head = (*qptr)->count = 0;
              (*qptr)->que = malloc(size * sizeof(int));
              (*qptr)->size = size;
    }
    else{
             /* delete the current queue */
             (void) free((*qptr)->que);
             (void) free(*qptr);
    }
}

I know that there's something fishy about the malloc part, but I'm having trouble seeing what's unsafe about this:

//que structure
typedef struct queue{
         int *que;  // the actual array of queue elements
         int head;   // the head index in que of the queue
         int count;  /// number of elements in queue
         int size; // max number of elements in queue
    } QUEUE;


void qManage(QUEUE **qptr, int flag, int size){
      if(flag){
              /* allocate a new queue */
              *qptr = malloc(sizeof(QUEUE));
              (*qptr)->head = (*qptr)->count = 0;
              (*qptr)->que = malloc(size * sizeof(int));
              (*qptr)->size = size;
    }
    else{
             /* delete the current queue */
             (void) free((*qptr)->que);
             (void) free(*qptr);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月下伊人醉2025-01-09 21:54:52

我很确定问题是这样的:
如果您为“size”传递负值会发生什么?

另一个可能的问题是您在分配后没有检查*qptr是否有NULL但是,这在实际代码中很少会出现问题,如果发生的话,您还需要担心其他错误。

I'm pretty sure the problem this:
What happens if you pass a negative value for 'size'?

Another possible issue is that you do not check *qptr for NULL after you allocate, however, rarely would that be a problem in actual code, if it would ever happen, you have other errors to worry about.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文