Fixed segfault/abort/double free with realloc in *_arrow()

This commit is contained in:
Jean Sirmai 2023-10-04 18:43:51 +02:00
parent 58f5cd8beb
commit 0587219bd3
Signed by: jean
GPG Key ID: FB3115C340E057E3
3 changed files with 76 additions and 52 deletions

View File

@ -27,6 +27,17 @@
#include <stdbool.h>
#include <GL/glu.h>
/*
* Structure describing an arrow
*/
typedef struct arrow_t {
GLuint load;
GLuint site;
GLuint x;
GLuint y;
GLuint z;
};
/*
* Writes basis and tip for all arrows into vertex and color buffers
*
@ -77,3 +88,4 @@ void write_arrow_lines_south_north (long s, int weight, int site);
* @return void
*/
void arrows_write_basis(long n);

View File

@ -46,7 +46,6 @@ struct gl_area_entry {
GLuint p; // init_shaders, draw
// GLuint line_indices_nb; // now : graphics.c > int buffer_lines_ndx
// GLuint plan_indices_nb; // now : graphics.c > int buffer_plans_ndx
};
/*

View File

@ -34,6 +34,7 @@
#define VERTEX_SHADER_FILE "src/shaders/shader.vert"
#define FRAG_SHADER_FILE "src/shaders/shader.frag"
static struct arrow_t *arrows;
static GLfloat *buffer_vertex_origin = NULL;
static GLfloat *buffer_colors_origin = NULL;
static GLuint *buffer_lines_origin = NULL;
@ -578,7 +579,7 @@ bool graphics_shutdown(const void *gl_area)
* - weight (or load)
* - coordinates in space (site, x, y, z)
*/
static void show_arrows_array (GLuint *arrows, int arrows_nb, int x, int y, int z)
static void show_arrows_array (struct arrow_t *arrows, int arrows_nb, int x, int y, int z)
{
printf("\n [rank] load | site x y z");
for (int i = 0; i < arrows_nb; i++)
@ -594,7 +595,7 @@ static void show_arrows_array (GLuint *arrows, int arrows_nb, int x, int y, int
* NB The space may be empty or saturated with arrows or any value in between
* Only one arrow per possible coordinates with a load max equal to ? TODO
*/
static void show_user_choices(GLuint *arrows, int arrows_nb,
static void show_user_choices(struct arrow_t *arrows, int arrows_nb,
int space_size_x, int space_size_y, int space_size_z,
int prefer, int show_all, int show_array)
{
@ -669,12 +670,12 @@ static void show_buffers_states(int space_X, int space_Y, int space_Z, int arrow
/*
* Assigns a new weight to the arrow at address (address)
*/
static int rewrite_arrow (GLuint *arrows, int arrows_nb, int address, int weight, int site, int x, int y, int z)
static int rewrite_arrow (int arrows_nb, int address, int weight, int site, int x, int y, int z)
{
*(arrows + address * 5 + 0) = weight;
arrows[address].load = weight;
// if (! TEST)
show_arrows_array (arrows, arrows_nb, x, y, z);
// show_arrows_array (arrows, arrows_nb, x, y, z);
return arrows_nb;
}
@ -683,22 +684,25 @@ static int rewrite_arrow (GLuint *arrows, int arrows_nb, int address, int weight
* Creates a new arrow at address (address)
* NB Weights (or loads) are NOT added
*/
static int create_arrow (GLuint *arrows, int arrows_nb, int weight, int site, int x, int y, int z)
static inline int create_arrow (int arrows_nb, int weight, int site, int x, int y, int z)
{
buffer_lines_origin = g_realloc(buffer_lines_origin, buffer_lines_size * sizeof(GLuint));
void *newptr = g_realloc(arrows, (arrows_nb + 1) * sizeof(struct arrow_t));
assert (buffer_lines_origin); assert (buffer_lines_size);
if (newptr)
arrows = newptr;
else
perror("Can't allocate new arrow buffer!\n");
*(arrows + arrows_nb * 5 + 0) = weight;
*(arrows + arrows_nb * 5 + 1) = site;
*(arrows + arrows_nb * 5 + 2) = x;
*(arrows + arrows_nb * 5 + 3) = y;
*(arrows + arrows_nb * 5 + 4) = z;
arrows[arrows_nb].load = weight;
arrows[arrows_nb].site = site;
arrows[arrows_nb].x = x;
arrows[arrows_nb].y = y;
arrows[arrows_nb].z = z;
arrows_nb ++;
// if (! TEST)
show_arrows_array (arrows, arrows_nb, x, y, z);
// show_arrows_array (arrows, arrows_nb, x, y, z);
return arrows_nb;
}
@ -706,28 +710,33 @@ static int create_arrow (GLuint *arrows, int arrows_nb, int weight, int site, in
/*
* Removes an arrow at address (address)
*/
static int erase_arrow (GLuint *arrows, int arrows_nb, int address, int site, int x, int y, int z)
static inline int erase_arrow (int arrows_nb, int address, int site, int x, int y, int z)
{
arrows_nb --;
if (arrows_nb > 0)
{
*(arrows + address + 0) = *(arrows + arrows_nb * 5 + 0);
*(arrows + address + 1) = *(arrows + arrows_nb * 5 + 1);
*(arrows + address + 2) = *(arrows + arrows_nb * 5 + 2);
*(arrows + address + 3) = *(arrows + arrows_nb * 5 + 3);
*(arrows + address + 4) = *(arrows + arrows_nb * 5 + 4);
arrows[address].load = arrows[arrows_nb].load;
arrows[address].site = arrows[arrows_nb].site;
arrows[address].x = arrows[arrows_nb].x;
arrows[address].y = arrows[arrows_nb].y;
arrows[address].z = arrows[arrows_nb].z;
}
buffer_lines_origin = g_realloc(buffer_lines_origin, buffer_lines_size * sizeof(GLuint));
void *newptr = g_realloc(arrows, arrows_nb * sizeof(struct arrow_t));
if (newptr)
arrows = newptr;
else
perror("Can't allocate new arrow buffer!\n");
// if (! TEST)
show_arrows_array (arrows, arrows_nb, x, y, z);
// show_arrows_array (arrows, arrows_nb, x, y, z);
return arrows_nb;
}
static void show_user_action(GLuint *arrows, int arrows_nb, int address, int requested_weight,
static inline void show_user_action(struct arrow_t *arrows, int arrows_nb, int address, int requested_weight,
int current_weight, int site, int x, int y, int z);
/*
* Calls one of the functions create_arrow(), erase_arrow() or rewrite_arrow()
@ -737,34 +746,34 @@ static void show_user_action(GLuint *arrows, int arrows_nb, int address, int req
* - Current_weight of an arrow located at the requested address == requested_weight
* - No arrow was found at the requested addres AND current_weight == requested_weight
*/
static int set_arrow (GLuint *arrows, int arrows_nb, int requested_weight, int site, int x, int y, int z)
static inline int set_arrow (struct arrow_t *arrows, int arrows_nb, int requested_weight, int site, int x, int y, int z)
{
int address = -1, current_weight = -1;
for (int i = 0; i < arrows_nb; i++) {
if ((site == *(arrows + i * 5 + 1))
&& (x == *(arrows + i * 5 + 2))
&& (y == *(arrows + i * 5 + 3))
&& (z == *(arrows + i * 5 + 4)))
if ((site == arrows[i].site)
&& (x == arrows[i].x)
&& (y == arrows[i].y)
&& (z == arrows[i].z))
{
address = i * 5;
current_weight = *(arrows + i * 5 + 0);
current_weight = arrows[i].load;
break;
}
}
printf("\nset_arrow() invoked with requested weight = %2d + ", requested_weight);
printf("\n[%d]set_arrow() invoked with requested weight = %2d + ", arrows_nb, requested_weight);
show_user_action(arrows, arrows_nb, address, requested_weight, current_weight, site, x, y, z);
if (address == -1 && requested_weight > 0)
return create_arrow (arrows, arrows_nb, requested_weight, site, x, y, z);
return create_arrow (arrows_nb, requested_weight, site, x, y, z);
if (address >= 0 && requested_weight == 0)
return erase_arrow (arrows, arrows_nb, address, site, x, y, z);
return erase_arrow (arrows_nb, address, site, x, y, z);
if (address >= 0 && current_weight != requested_weight)
return rewrite_arrow (arrows, arrows_nb, address/5, requested_weight, site, x, y, z);
return rewrite_arrow (arrows_nb, address/5, requested_weight, site, x, y, z);
if (! TEST && address >= 0 && current_weight == requested_weight) show_arrows_array(arrows, arrows_nb, x, y, z);
@ -820,7 +829,7 @@ static int set_arrow (GLuint *arrows, int arrows_nb, int requested_weight, int s
* Prints the result of the function set_arrow()
* and indicates the reasons of the choice (call) this function makes (see this function)
*/
static void show_user_action(GLuint *arrows, int arrows_nb, int address, int requested_weight,
static void show_user_action(struct arrow_t *arrows, int arrows_nb, int address, int requested_weight,
int current_weight, int site, int x, int y, int z)
{
if (address == -1 && requested_weight > 0) { // *(arrows + i * 5 + 0)
@ -870,26 +879,28 @@ void main_test_graphics (void)
int rand(void);
void srand(unsigned int seed); // printf ("Valeur max : %d\n", RAND_MAX); min + rand() % (max+1 - min);
int arbitrary = 5;
int arbitrary = 500;
int space_X = 1 + rand() % arbitrary,
space_Y = 1 + rand() % arbitrary,
space_Z = 1 + rand() % arbitrary;
int density_max = space_X * space_Y * space_Z;
int arrows_nb = rand() % density_max / 3;
int arrows_nb = 0;
int pref_show_grids = 0; // 0, 1, 2, 3, 5, 6, 10, 15, 30, etc
// xyz, 0, x, y, z, xy, xz, yz, xyz
GLuint arrows[arrows_nb * 5];
/* arrows = g_malloc0(arrows_nb * sizeof(struct arrow_t)); */
show_user_choices(arrows, arrows_nb, space_X, space_Y, space_Z, pref_show_grids, 1, 0);
// WARNING arrows with same address will be drawn superimposed
if (arrows_nb > 0)
for (int i = 0; i < arrows_nb; i ++) {
arrows[i * 5 + 0] = 1 + rand() % arbitrary; // load / weight
arrows[i * 5 + 1] = rand() % 6; // site
arrows[i * 5 + 2] = rand() % space_X; // x
arrows[i * 5 + 3] = rand() % space_Y; // y
arrows[i * 5 + 4] = rand() % space_Z; // z
};
/* if (arrows_nb > 0) */
/* for (int i = 0; i < arrows_nb; i ++) { */
/* arrows[i].load = 1 + rand() % arbitrary; */
/* arrows[i].site = rand() % 6; */
/* arrows[i].x = rand() % space_X; */
/* arrows[i].y = rand() % space_Y; */
/* arrows[i].z = rand() % space_Z; */
/* }; */
// forget the next two lines during the random tests
// arrows[] = {... some values ...}
@ -911,7 +922,7 @@ void main_test_graphics (void)
/* Therefore: if (arrows_nb > 0) is not required (next line) */
arrows_write_terminations (space_X, space_Y, space_Z);
for (int k = 0; k < 2; k++) // for (int k = 0; k < arbitrary; k++) TODO
for (int k = 0; k < arbitrary; k++)
arrows_nb = set_arrow (arrows, arrows_nb,
rand() % arbitrary, // load / weight
rand() % 6, // site,
@ -927,11 +938,11 @@ void main_test_graphics (void)
if (arrows_nb > 0)
for (int i = 0; i < arrows_nb; i++) {
weight = arrows[i * 5 + 0];
site = arrows[i * 5 + 1];
x = arrows[i * 5 + 2];
y = arrows[i * 5 + 3];
z = arrows[i * 5 + 4];
weight = arrows[i].load;
site = arrows[i].site;
x = arrows[i].x;
y = arrows[i].y;
z = arrows[i].z;
offset_up_to_this_cube_coords = 12 * (stx * x + sty * y + stz * z);
arrow_offset = offset_after_grids + offset_up_to_this_cube_coords;
@ -946,6 +957,8 @@ void main_test_graphics (void)
}
}
free(arrows);
show_buffers_states(space_X, space_Y, space_Z, arrows_nb,
offset_after_grids, buffer_vertex_size,
buffer_lines_size_after_cubes, buffer_lines_size);