winsys/virgl/vtest: Correct off-by-one error in resource allocation
authorGert Wollny <gert.wollny@collabora.com>
Tue, 4 Sep 2018 07:36:53 +0000 (09:36 +0200)
committerGert Wollny <gw.fossdev@gmail.com>
Wed, 5 Sep 2018 11:54:01 +0000 (13:54 +0200)
The resource bo array must already extended when the target index is
equal to the current size of the array.

Signed-off-by: Gert Wollny <gert.wollny@collabora.com>
src/gallium/winsys/virgl/vtest/virgl_vtest_winsys.c

index 9a96c6eb83f5753b66eaba04f34b4b231b5c8bc2..6c03a6b359401ea6db00fb473b6b0d7d8f32dd4b 100644 (file)
@@ -459,13 +459,18 @@ static void virgl_vtest_add_res(struct virgl_vtest_winsys *vtws,
 {
    unsigned hash = res->res_handle & (sizeof(cbuf->is_handle_added)-1);
 
-   if (cbuf->cres > cbuf->nres) {
-      cbuf->nres += 256;
-      cbuf->res_bo = realloc(cbuf->res_bo, cbuf->nres * sizeof(struct virgl_hw_buf*));
-      if (!cbuf->res_bo) {
+   if (cbuf->cres >= cbuf->nres) {
+      unsigned new_nres = cbuf->nres + 256;
+      struct virgl_hw_res **new_re_bo = REALLOC(cbuf->res_bo,
+                                                cbuf->nres * sizeof(struct virgl_hw_buf*),
+                                                new_nres * sizeof(struct virgl_hw_buf*));
+      if (!new_re_bo) {
           fprintf(stderr,"failure to add relocation %d, %d\n", cbuf->cres, cbuf->nres);
           return;
       }
+
+      cbuf->res_bo = new_re_bo;
+      cbuf->nres = new_nres;
    }
 
    cbuf->res_bo[cbuf->cres] = NULL;