Skip to content

Commit

Permalink
UPBGE: Fix for #797 (#869)
Browse files Browse the repository at this point in the history
Introduce a new array of GPUType sizes to avoid a buffer overrun when we introduce new elements in GPUType.
The new array will allow make the system extensible easier.
More comments added.

P.D. It fixes the win32 crash.
  • Loading branch information
lordloki authored Oct 9, 2018
1 parent 006d60e commit 683c104
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
4 changes: 3 additions & 1 deletion source/blender/gpu/GPU_material.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ typedef struct GPUParticleInfo GPUParticleInfo;
/* Functions to create GPU Materials nodes */

typedef enum GPUType {
/* Types taken into account by GPU_DATATYPE_STR and GPU_DATATYPE_SIZE arrays */
/* The value indicates the number of elements in each type */
GPU_NONE = 0,
GPU_FLOAT = 1,
Expand All @@ -77,7 +78,8 @@ typedef enum GPUType {
GPU_MAT3 = 9,
GPU_MAT4 = 16,

GPU_INT = 17,
GPU_INT = 17, /* this value doesn't point the number of elements */
/* end of types taken into account by GPU_DATATYPE_STR and GPU_DATATYPE_SIZE arrays */

GPU_TEX2D = 1002,
GPU_SHADOW2D = 1003,
Expand Down
9 changes: 7 additions & 2 deletions source/blender/gpu/intern/gpu_codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
#include <string.h>
#include <stdarg.h>


extern char datatoc_gpu_shader_material_glsl[];
extern char datatoc_gpu_shader_vertex_glsl[];
extern char datatoc_gpu_shader_vertex_world_glsl[];
Expand Down Expand Up @@ -90,6 +91,10 @@ static const char *GPU_DATATYPE_STR[18] = {
"", "float", "vec2", "vec3", "vec4",
NULL, NULL, NULL, NULL, "mat3", NULL, NULL, NULL, NULL, NULL, NULL, "mat4", "int"
};
/* Indices match the GPUType size */
static const unsigned int GPU_DATATYPE_SIZE[18] = {
0, 1, 2, 3, 4, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0, 0, 16, 1
};

/* GLSL code parsing for finding function definitions.
* These are stored in a hash for lookup when creating a material. */
Expand Down Expand Up @@ -172,7 +177,7 @@ static void gpu_parse_functions_string(GHash *hash, char *code)

/* test for type */
type = GPU_NONE;
for (i = 1; i <= 17; i++) {
for (i = 1; i < ARRAY_SIZE(GPU_DATATYPE_STR); i++) {
if (GPU_DATATYPE_STR[i] && gpu_str_prefix(code, GPU_DATATYPE_STR[i])) {
type = i;
break;
Expand Down Expand Up @@ -1297,7 +1302,7 @@ static void gpu_node_input_link(GPUNode *node, GPUNodeLink *link, const GPUType
input->type = type;
input->source = GPU_SOURCE_VEC_UNIFORM;

memcpy(input->vec, link->ptr1, type * sizeof(float));
memcpy(input->vec, link->ptr1, GPU_DATATYPE_SIZE[type] * sizeof(float));
if (link->dynamic) {
input->dynamicvec = link->ptr1;
input->dynamictype = link->dynamictype;
Expand Down

0 comments on commit 683c104

Please sign in to comment.