Commit 228f9dbf authored by Arnaud Blanchard's avatar Arnaud Blanchard
Browse files

Fix possible segfault if blc_print_* are called with nullptr message

parent 7a0e958b
......@@ -14,7 +14,7 @@ cmake_minimum_required(VERSION 2.6)
project(blc_core)
set(CMAKE_MACOSX_RPATH 0) #avoid warning in MACOSX
add_definitions(-Wno-multichar -pthread)
add_definitions(-std=c++14 -Wno-multichar -pthread)
include_directories(include)
set(sources
......
......@@ -50,9 +50,18 @@ typedef struct blc_array
blc_array();
/**Define and allocates the array */
blc_array(uint32_t type, uint32_t format, int dims_nb, int length0, ...);
/**Copy the definition of the array, NOT the content */
blc_array(blc_array const &array);
/**Move the array. The content of initial array is not valid anymore*/
blc_array(blc_array &&array);
/**Free dims. The data is freed by ~blc_mem()*/
~blc_array();
/**Copy the definition of the array, NOT the content */
blc_array& operator=(blc_array const &other);
/* Definiting the array without allocation. Useful when you want to use external buffer
====================================================================================*/
......
......@@ -53,6 +53,17 @@ typedef struct blc_mem {
blc_mem();
/** Create and allocate memory of size size */
blc_mem(size_t size);
///Copy the defienition of blc_meme, NOT the content
blc_mem(blc_mem const &mem);
///Move the definition and move the content
blc_mem(blc_mem &&mem);
///Copy the defienition of blc_meme, NOT the content
blc_mem &operator=(blc_mem const &other);
/// Free the data. Only if data is not NULL and size !=0; It is a way to not delete data we want to keep.
~blc_mem();
/**really allocates the data of size previously set*/
......
......@@ -21,6 +21,7 @@
#include <signal.h> //raise
#include <stdio.h> //fopen
#include <algorithm> //copy
START_EXTERN_C
blc_dim *vcreate_blc_dims(size_t *size, uint32_t type, int dims_nb, int length, va_list arguments){
......@@ -61,6 +62,29 @@ blc_array::blc_array(uint32_t type, uint32_t format, int dims_nb, int length0, .
va_end(arguments);
}
blc_array::blc_array(blc_array const &array):blc_mem(array), type(array.type), format(array.format), dims_nb(array.dims_nb), total_length(array.total_length){
dims=new blc_dim[dims_nb];
std::copy(array.dims, array.dims+dims_nb, dims);
}
blc_array::blc_array(blc_array &&array):blc_mem(array){
dims=array.dims;
}
blc_array& blc_array::operator=(blc_array const &other){
if (this==&other) return *this;
type=other.type;
format=other.format;
total_length=other.total_length;
dims_nb=other.dims_nb;
MANY_REALLOCATIONS(&dims, dims_nb);
std::copy(other.dims, other.dims+dims_nb, dims);
return *this;
}
blc_array::~blc_array(){
if (dims) FREE(dims);
}
......
......@@ -19,6 +19,7 @@
//
#include "blc_mem.h"
#include <algorithm> //std::copy
blc_mem::blc_mem():data(NULL), size(0){};
......@@ -27,6 +28,20 @@ blc_mem::blc_mem(size_t size)
data = MANY_ALLOCATIONS(size, char);
}
blc_mem::blc_mem(blc_mem const &mem):data(nullptr), size(mem.size){
};
blc_mem::blc_mem(blc_mem &&mem):size(mem.size){
chars=mem.chars;
};
blc_mem &blc_mem::operator=(blc_mem const &other){
if (data!=nullptr) FREE(data);
size=other.size;
return *this;
};
blc_mem::~blc_mem(){
if ((data) && (size))
{
......
......@@ -100,7 +100,7 @@ void blc_fatal_system_error(const char *name_of_file, const char* name_of_functi
va_start(arguments, message);
color_eprintf(BLC_BRIGHT_RED, "\n%s: %s \t %i:%s\n", blc_program_id, name_of_file, numero_of_line, name_of_function);
color_eprintf(BLC_BRIGHT_RED, "System error: %s\n", strerror(errno));
color_veprintf(BLC_BRIGHT_RED, message, arguments);
if (message) color_veprintf(BLC_BRIGHT_RED, message, arguments);
va_end(arguments);
fprintf(stderr, "\n");
fflush(stderr);
......@@ -115,7 +115,7 @@ void blc_fatal_command_system_error(const char *name_of_file, const char* name_o
color_eprintf(BLC_BRIGHT_RED, "\n%s: %s \t %i:%s\n", blc_program_id, name_of_file, numero_of_line, name_of_function);
fprintf(stderr, "Executing: %s\n", command);
color_eprintf(BLC_BRIGHT_RED, "System error: %s\n", strerror(errno));
color_veprintf(BLC_BRIGHT_RED, message, arguments);
if (message) color_veprintf(BLC_BRIGHT_RED, message, arguments);
va_end(arguments);
fprintf(stderr, "\n");
fflush(stderr);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment