Skip to content

Commit

Permalink
Segmentation fault fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Oct 2, 2017
1 parent cfc5254 commit e3f0714
Show file tree
Hide file tree
Showing 6 changed files with 210 additions and 205 deletions.
2 changes: 1 addition & 1 deletion 4d_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ static int pdo_4d_handle_factory(pdo_dbh_t *dbh, zval *driver_options TSRMLS_DC)
{ "host", "localhost", 0 },
{ "port", "19812", 0 },
{ "dbname", "", 0 },
{ "charset", "UTF-8", 0 },
{ "charset", "UTF-16LE", 0 },
{ "user", "", 0 },
{ "password", "", 0 },
};
Expand Down
109 changes: 66 additions & 43 deletions 4d_statement.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,38 +42,37 @@

static int pdo_4d_stmt_execute(pdo_stmt_t *stmt TSRMLS_DC)
{
pdo_4d_stmt *S;
pdo_4d_db_handle *H;
FOURD_LONG8 row_count;
unsigned int page_size=65535;
S = (pdo_4d_stmt*)stmt->driver_data;
H = S->H;
if (S->result) {
fourd_free_result(S->result);
S->result = NULL;
}
if(S->state==NULL) { /* if statement has not prepared */
if ((S->result=fourd_query(H->server, stmt->active_query_string)) ==NULL) {
pdo_4d_error_stmt(stmt);
return 0;
}
}
else { /* if statement has prepared */
if ((S->result=fourd_exec_statement(S->state, page_size)) ==NULL) {
pdo_4d_error_stmt(stmt);
return 0;
}
}
if ((row_count = fourd_affected_rows(H->server)) == (FOURD_LONG8)-1) {

stmt->row_count = fourd_num_rows(S->result);
stmt->column_count = fourd_num_columns(S->result);
}
else {
/* this was a DML or DDL query (INSERT, UPDATE, DELETE, ... */
stmt->row_count = row_count;
}
return 1;
pdo_4d_stmt *S;
pdo_4d_db_handle *H;
FOURD_LONG8 row_count;
S = (pdo_4d_stmt*)stmt->driver_data;
H = S->H;
if (S->result) {
fourd_free_result(S->result);
S->result = NULL;
}
if(S->state==NULL) { /* if statement has not prepared */
if ((S->result=fourd_query(H->server, stmt->active_query_string)) ==NULL) {
pdo_4d_error_stmt(stmt);
return 0;
}
}
else { /* if statement has prepared */
if ((S->result=fourd_exec_statement(S->state)) ==NULL) {
pdo_4d_error_stmt(stmt);
return 0;
}
}
if ((row_count = fourd_affected_rows(H->server)) == (FOURD_LONG8)-1) {

stmt->row_count = fourd_num_rows(S->result);
stmt->column_count = fourd_num_columns(S->result);
}
else {
/* this was a DML or DDL query (INSERT, UPDATE, DELETE, ... */
stmt->row_count = row_count;
}
return 1;
}

static int pdo_4d_stmt_describe(pdo_stmt_t *stmt, int colno TSRMLS_DC)
Expand All @@ -97,19 +96,23 @@ static int pdo_4d_stmt_describe(pdo_stmt_t *stmt, int colno TSRMLS_DC)
if (cols[0].name) {
return 1;
}



for (i=0; i < stmt->column_count; i++) {
int namelen;
const char *name=fourd_get_column_name(S->result,i);
namelen = strlen(name);
cols[i].precision = 0;
cols[i].maxlen = namelen;/*namelen;*/
#if PHP_VERSION_ID >= 70000
cols[i].name = zend_string_init(name, namelen, 0);
#else
cols[i].namelen = namelen;
cols[i].name = estrndup(name, namelen);
#endif
cols[i].name = estrndup(name, namelen);
cols[i].param_type = PDO_PARAM_STR;
cols[i].param_type = PDO_PARAM_STR;
//cols[i].param_type = PDO_PARAM_STR;
//if(i==1) cols[i].param_type = PDO_PARAM_LOB;
}
return 1;
Expand All @@ -132,7 +135,7 @@ static int pdo_4d_stmt_get_col(pdo_stmt_t *stmt, int colno, char **ptr, unsigned
{
case VK_STRING:
/* convert into desired charset */
*ptr=php_mb_convert_encoding(*ptr, *len,S->charset,FOURD_CHARSET_SERVEUR,len TSRMLS_CC);
//*ptr=php_mb_convert_encoding(*ptr, *len,S->charset,FOURD_CHARSET_SERVEUR,len TSRMLS_CC);
break;
case VK_BLOB:
case VK_IMAGE:
Expand All @@ -155,7 +158,7 @@ static int pdo_4d_stmt_get_col(pdo_stmt_t *stmt, int colno, char **ptr, unsigned
break;
default:
/* convert into desired charset from "ISO-8859-1" for not VK_STRING,VK_BLOB or VK_IMAGE data */
*ptr=php_mb_convert_encoding(*ptr, *len,S->charset,"ISO-8859-1",len TSRMLS_CC);
//*ptr=php_mb_convert_encoding(*ptr, *len,S->charset,"ISO-8859-1",len TSRMLS_CC);
break;
}

Expand All @@ -164,8 +167,12 @@ static int pdo_4d_stmt_get_col(pdo_stmt_t *stmt, int colno, char **ptr, unsigned
static int pdo_4d_stmt_fetch(pdo_stmt_t *stmt, enum pdo_fetch_orientation ori, long offset TSRMLS_DC)
{
pdo_4d_stmt *S = (pdo_4d_stmt*)stmt->driver_data;
size_t len;

if (!S->result) {
//char* test = php_mb_convert_encoding("TEST", 4, "UTF-8", "UTF-16LE", len);


if (!S->result) {
strcpy(stmt->error_code, "HY000");
return 0;
}
Expand Down Expand Up @@ -194,6 +201,7 @@ static int pdo_4d_stmt_get_attribute(pdo_stmt_t *stmt, long attr, zval *return_v
{
//pdo_4d_db_handle *H = (pdo_4d_db_handle *)dbh->driver_data;
pdo_4d_stmt *S = (pdo_4d_stmt*)stmt->driver_data;

switch (attr) {
case PDO_FOURD_ATTR_CHARSET:

Expand Down Expand Up @@ -440,7 +448,11 @@ static int pdo_4d_stmt_param_hook(pdo_stmt_t *stmt, struct pdo_bound_param_data
/* MBSTRING_API char * php_mb_convert_encoding(char *input, size_t length, char *_to_encoding, char *_from_encodings, size_t *output_len TSRMLS_DC) */

#if PHP_VERSION_ID >= 70000
char* val=php_mb_convert_encoding(Z_STRVAL(param->parameter), Z_STRLEN(param->parameter),FOURD_CHARSET_SERVEUR,S->charset,&len TSRMLS_CC);
//char* val=php_mb_convert_encoding(Z_STRVAL(param->parameter), Z_STRLEN(param->parameter),FOURD_CHARSET_SERVEUR,S->charset,&len TSRMLS_CC);

char* val = Z_STRVAL(param->parameter);
len = Z_STRLEN(param->parameter);

#else
char* val=php_mb_convert_encoding(Z_STRVAL_P(param->parameter), Z_STRLEN_P(param->parameter),FOURD_CHARSET_SERVEUR,S->charset,&len TSRMLS_CC);
#endif
Expand All @@ -454,18 +466,29 @@ static int pdo_4d_stmt_param_hook(pdo_stmt_t *stmt, struct pdo_bound_param_data
{
FOURD_STRING str;
size_t len=0;
char* val=NULL;
char* val;
size_t test_length = 0;
char* test_value;

#if PHP_VERSION_ID >= 70000
convert_to_string(&param->parameter);
val=php_mb_convert_encoding(Z_STRVAL(param->parameter), Z_STRLEN(param->parameter),FOURD_CHARSET_SERVEUR,S->charset,&len TSRMLS_CC);
convert_to_string(&param->parameter);
test_value = Z_STRVAL(param->parameter);
test_length = Z_STRLEN(param->parameter);

val = test_value;
len = Z_STRLEN(param->parameter);

//val = php_mb_convert_encoding(test_value, test_length,FOURD_CHARSET_SERVEUR,S->charset,&len TSRMLS_CC);

//PHPWRITE(Z_STRVAL(param->parameter), Z_STRLEN(param->parameter));
#else
convert_to_string(param->parameter);
val=php_mb_convert_encoding(Z_STRVAL_P(param->parameter), Z_STRLEN_P(param->parameter),FOURD_CHARSET_SERVEUR,S->charset,&len TSRMLS_CC);
#endif

str.length=len/2;
str.data=val;
str.length = len/2;
str.data = val;

fourd_bind_param(S->state,param->paramno,VK_STRING, &str);
}
return 1;
Expand Down
34 changes: 18 additions & 16 deletions lib4d_sql/fourd.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,16 @@ const char * fourd_error(FOURD *cnx)
}
int fourd_exec(FOURD *cnx,const char *query)
{
return _query(cnx,3,query,NULL,cnx->preferred_image_types,100);
return _query(cnx,3,query,NULL,cnx->preferred_image_types);
}
FOURD_RESULT* fourd_query(FOURD *cnx,const char *query)
{
FOURD_RESULT* result;

result=calloc(1,sizeof(FOURD_RESULT));
result->cnx=cnx;
if(_query(cnx,3,query,result,cnx->preferred_image_types,100)==0)

if(_query(cnx,3,query,result,cnx->preferred_image_types)==0)
{
result->numRow=-1;
return result;
Expand Down Expand Up @@ -430,21 +431,22 @@ int fourd_bind_param(FOURD_STATEMENT *state,unsigned int numParam,FOURD_TYPE typ
}
return 0;
}
FOURD_RESULT *fourd_exec_statement(FOURD_STATEMENT *state, int res_size)

FOURD_RESULT *fourd_exec_statement(FOURD_STATEMENT *state)
{
FOURD_RESULT *result=NULL;
result=calloc(1,sizeof(FOURD_RESULT));
result->cnx=state->cnx;
if(_query_param(state->cnx,6,state->query,state->nb_element,state->elmt,result,state->preferred_image_types,res_size)==0)
{
result->numRow=-1;
return result;
}
else
{
fourd_free_result(result);
return NULL;
}
FOURD_RESULT *result=NULL;
result=calloc(1,sizeof(FOURD_RESULT));
result->cnx=state->cnx;
if(_query_param(state->cnx,6,state->query,state->nb_element,state->elmt,result,state->preferred_image_types)==0)
{
result->numRow=-1;
return result;
}
else
{
fourd_free_result(result);
return NULL;
}
}
void fourd_set_preferred_image_types(FOURD* cnx,const char *types)
{
Expand Down
8 changes: 4 additions & 4 deletions lib4d_sql/fourd.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ typedef struct in_addr IN_ADDR;
#define VERBOSE 0


/* taille maximal de 2K pour les envoi de requÍte */
/* taille maximal de 2K pour les envoi de requ�te */
/* #define BUFFER_LENGTH 131072 */
/* taille maximal de 128K pour les rÈponse */
/* taille maximal de 128K pour les r�ponse */
#define BUFFER_LENGTH 131072
#define ERROR_STRING_LENGTH 2048

Expand Down Expand Up @@ -140,7 +140,7 @@ typedef double FOURD_REAL;
typedef struct{int exp;unsigned char sign;int data_length;void* data;}FOURD_FLOAT;
typedef struct{short year;unsigned char mounth;unsigned char day;unsigned int milli;}FOURD_TIMESTAMP;
typedef long long FOURD_DURATION;//in milliseconds
typedef struct{int length;unsigned char *data;}FOURD_STRING;
typedef struct{int length;char *data;}FOURD_STRING;
typedef struct{int length;void *data;}FOURD_BLOB;
/* typedef struct{}FOURD_IMAGE; */

Expand Down Expand Up @@ -283,7 +283,7 @@ int fourd_field_to_string(FOURD_RESULT *res,unsigned int numCol,char **value,siz

FOURD_STATEMENT * fourd_prepare_statement(FOURD *cnx,const char *query);
int fourd_bind_param(FOURD_STATEMENT *state,unsigned int numParam,FOURD_TYPE type, void *val);
FOURD_RESULT *fourd_exec_statement(FOURD_STATEMENT *state, int res_size);
FOURD_RESULT *fourd_exec_statement(FOURD_STATEMENT *state);

void fourd_set_preferred_image_types(FOURD* cnx,const char *types);
void fourd_set_statement_preferred_image_types(FOURD_STATEMENT *state,const char *types);
Expand Down
4 changes: 2 additions & 2 deletions lib4d_sql/fourd_int.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ int dblogin(FOURD *cnx,unsigned short int id_cnx,const char *user,const char*pwd
int dblogout(FOURD *cnx,unsigned short int id_cmd);
int quit(FOURD *cnx,unsigned short int id_cmd);
//return 0 for OK et -1 for no readable header and error_code
int _query(FOURD *cnx,unsigned short int id_cmd,const char *request,FOURD_RESULT *result,const char*image_type, int res_size);
int _query(FOURD *cnx,unsigned short int id_cmd,const char *request,FOURD_RESULT *result,const char*image_type);
int __fetch_result(FOURD *cnx,unsigned short int id_cmd,int statement_id,int command_index,unsigned int first_row,unsigned int last_row,FOURD_RESULT *result);
int _fetch_result(FOURD_RESULT *res,unsigned short int id_cmd);
int get(const char* msg,const char* section,char *valeur,int max_length);
Expand All @@ -71,7 +71,7 @@ void _clear_atrr_cnx(FOURD *cnx);
int close_statement(FOURD_RESULT *res,unsigned short int id_cmd);

int _prepare_statement(FOURD *cnx,unsigned short int id_cmd,const char *request);
int _query_param(FOURD *cnx,unsigned short int id_cmd, const char *request,unsigned int nbParam, const FOURD_ELEMENT *param,FOURD_RESULT *result,const char*image_type, int res_size);
int _query_param(FOURD *cnx,unsigned short int id_cmd, const char *request,unsigned int nbParam, const FOURD_ELEMENT *param,FOURD_RESULT *result,const char*image_type);
int _is_multi_query(const char *request);
int _valid_query(FOURD *cnx,const char *request);
/*********************/
Expand Down
Loading

0 comments on commit e3f0714

Please sign in to comment.