Skip to content

Commit

Permalink
Replace String Lengths with Constants ERROR_STRING_LENGTH and MAX_HEA…
Browse files Browse the repository at this point in the history
…DER_SIZE. Increase MAX_HEADER_SIZE.
  • Loading branch information
bendailey committed Dec 1, 2015
1 parent 27e87c9 commit 3b8f37d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 41 deletions.
22 changes: 11 additions & 11 deletions lib4d_sql/communication.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ int socket_connect(FOURD *cnx,const char *host,unsigned int port)
if ( iResult != 0 ) {
Printf("getaddrinfo failed: %d : %s\n", iResult,gai_strerror(iResult));
cnx->error_code=-iResult;
strncpy_s(cnx->error_string,2048,gai_strerror(iResult),2048);
strncpy_s(cnx->error_string,ERROR_STRING_LENGTH,gai_strerror(iResult),ERROR_STRING_LENGTH);
return 1;
}
//Printf("getaddrinfo ok\n");
Expand All @@ -101,7 +101,7 @@ int socket_connect(FOURD *cnx,const char *host,unsigned int port)
if (cnx->socket == INVALID_SOCKET) {
Printf("Error at socket(): %ld\n", WSAGetLastError());
cnx->error_code=-WSAGetLastError();
strncpy_s(cnx->error_string,2048,"Unable to create socket",2048);
strncpy_s(cnx->error_string,ERROR_STRING_LENGTH,"Unable to create socket",ERROR_STRING_LENGTH);
freeaddrinfo(result);
return 1;
}
Expand All @@ -111,7 +111,7 @@ int socket_connect(FOURD *cnx,const char *host,unsigned int port)
if (iResult == SOCKET_ERROR) {
Printf("Error at socket(): %ld\n", WSAGetLastError());
cnx->error_code=-WSAGetLastError();
strncpy_s(cnx->error_string,2048,"Unable to connect to server",2048);
strncpy_s(cnx->error_string,ERROR_STRING_LENGTH,"Unable to connect to server",ERROR_STRING_LENGTH);
freeaddrinfo(result);
closesocket(cnx->socket);
cnx->socket = INVALID_SOCKET;
Expand All @@ -132,7 +132,7 @@ int socket_connect(FOURD *cnx,const char *host,unsigned int port)
if (cnx->socket == INVALID_SOCKET) {
Printf("Unable to connect to server!\n");
cnx->error_code=-1;
strncpy_s(cnx->error_string,2048,"Unable to connect to server",2048);
strncpy_s(cnx->error_string,ERROR_STRING_LENGTH,"Unable to connect to server",ERROR_STRING_LENGTH);
return 1;
}
//Printf("fin de la fonction\n");
Expand Down Expand Up @@ -232,7 +232,7 @@ int socket_receiv_data(FOURD *cnx,FOURD_RESULT *state)
unsigned int nbCol=state->row_type.nbColumn;
unsigned int nbRow=state->row_count_sent;
unsigned int r,c;
FOURD_TYPE *colType;
FOURD_TYPE *colType=NULL;
FOURD_ELEMENT *pElmt=NULL;
char status_code=0;
int elmt_size=0;
Expand Down Expand Up @@ -519,7 +519,7 @@ int socket_connect_timeout(FOURD *cnx,const char *host,unsigned int port,int tim
if ( iResult != 0 ) {
Printf("getaddrinfo failed: %d : %s\n", iResult,gai_strerror(iResult));
cnx->error_code=-iResult;
strncpy_s(cnx->error_string,2048,gai_strerror(iResult),2048);
strncpy_s(cnx->error_string,ERROR_STRING_LENGTH,gai_strerror(iResult),ERROR_STRING_LENGTH);
return 1;
}
/* Printf("getaddrinfo ok\n"); */
Expand All @@ -534,7 +534,7 @@ int socket_connect_timeout(FOURD *cnx,const char *host,unsigned int port,int tim
if (cnx->socket == INVALID_SOCKET) {
Printf("Error at socket(): %ld\n", WSAGetLastError());
cnx->error_code=-WSAGetLastError();
strncpy_s(cnx->error_string,2048,"Unable to create socket",2048);
strncpy_s(cnx->error_string,ERROR_STRING_LENGTH,"Unable to create socket",ERROR_STRING_LENGTH);
freeaddrinfo(result);
return 1;
}
Expand All @@ -555,7 +555,7 @@ int socket_connect_timeout(FOURD *cnx,const char *host,unsigned int port,int tim
if (valopt) {
fprintf(stderr, "Error in connection() %d - %s\n", valopt, strerror(valopt));
cnx->error_code=valopt;
strncpy_s(cnx->error_string,2048,strerror(valopt),2048);
strncpy_s(cnx->error_string,ERROR_STRING_LENGTH,strerror(valopt),ERROR_STRING_LENGTH);
freeaddrinfo(result);
closesocket(cnx->socket);
cnx->socket = INVALID_SOCKET;
Expand All @@ -566,7 +566,7 @@ int socket_connect_timeout(FOURD *cnx,const char *host,unsigned int port,int tim
else {
/*fprintf(stderr, "Timeout or error() %d - %s\n", valopt, strerror(valopt)); */
cnx->error_code=3011;
strncpy_s(cnx->error_string,2048,"Connect timed out",2048);
strncpy_s(cnx->error_string,ERROR_STRING_LENGTH,"Connect timed out",ERROR_STRING_LENGTH);
freeaddrinfo(result);
closesocket(cnx->socket);
cnx->socket = INVALID_SOCKET;
Expand All @@ -576,7 +576,7 @@ int socket_connect_timeout(FOURD *cnx,const char *host,unsigned int port,int tim
else {
/*fprintf(stderr, "Error connecting %d - %s\n", errno, strerror(errno)); */
cnx->error_code=-WSAGetLastError();
strncpy_s(cnx->error_string,2048,"Error connecting",2048);
strncpy_s(cnx->error_string,ERROR_STRING_LENGTH,"Error connecting",ERROR_STRING_LENGTH);
freeaddrinfo(result);
closesocket(cnx->socket);
cnx->socket = INVALID_SOCKET;
Expand All @@ -603,7 +603,7 @@ int socket_connect_timeout(FOURD *cnx,const char *host,unsigned int port,int tim
if (cnx->socket == INVALID_SOCKET) {
Printf("Unable to connect to server!\n");
cnx->error_code=-1;
strncpy_s(cnx->error_string,2048,"Unable to connect to server",2048);
strncpy_s(cnx->error_string,ERROR_STRING_LENGTH,"Unable to connect to server",ERROR_STRING_LENGTH);
return 1;
}
/* Printf("fin de la fonction\n"); */
Expand Down
20 changes: 10 additions & 10 deletions lib4d_sql/fourd.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,15 @@ int fourd_connect(FOURD *cnx,const char *host,const char *user,const char *passw
//not init
Printferr("Erreur: FOURD object did not initialised\n");
cnx->error_code=-1;
strncpy_s(cnx->error_string,2048,"FOURD object did not initialised",2048);
strncpy_s(cnx->error_string,ERROR_STRING_LENGTH,"FOURD object did not initialised",ERROR_STRING_LENGTH);
return 1;
}
if(cnx->connected)
{
//deja connecter
Printferr("Erreur: already connected\n");
cnx->error_code=-1;
strncpy_s(cnx->error_string,2048,"Already connected",2048);
strncpy_s(cnx->error_string,ERROR_STRING_LENGTH,"Already connected",ERROR_STRING_LENGTH);
return 1;
}
if(socket_connect_timeout(cnx,host,port,15))
Expand All @@ -81,7 +81,7 @@ int fourd_connect(FOURD *cnx,const char *host,const char *user,const char *passw
Printferr("Erreur in socket_connect\n");
cnx->connected=0;
//cnx->error_code=-1;
//strncpy_s(cnx->error_string,2048,"Error during connection",2048);
//strncpy_s(cnx->error_string,ERROR_STRING_LENGTH,"Error during connection",ERROR_STRING_LENGTH);
return 1;
}
if(login(cnx,1,user,((password==NULL)?"":password),cnx->preferred_image_types)!=0)
Expand All @@ -91,14 +91,14 @@ int fourd_connect(FOURD *cnx,const char *host,const char *user,const char *passw
cnx->connected=0;
if(cnx->error_code==0) {
cnx->error_code=-1;
strncpy_s(cnx->error_string,2048,"Error during login",2048);
strncpy_s(cnx->error_string,ERROR_STRING_LENGTH,"Error during login",ERROR_STRING_LENGTH);
}
return 1;
}
cnx->connected=1;
//Printferr("Erreur: not erreur\n");
cnx->error_code=0;
strncpy_s(cnx->error_string,2048,"",2048);
strncpy_s(cnx->error_string,ERROR_STRING_LENGTH,"",ERROR_STRING_LENGTH);
return 0;
}
int fourd_close(FOURD *cnx)
Expand Down Expand Up @@ -220,12 +220,12 @@ void * fourd_field(FOURD_RESULT *res,unsigned int numCol)

if(res->numRow>=nbRow){
res->cnx->error_code=-1;
sprintf_s(res->cnx->error_string,2048,"num Row out of bounds",2048);
sprintf_s(res->cnx->error_string,ERROR_STRING_LENGTH,"num Row out of bounds",ERROR_STRING_LENGTH);
return NULL;
}
if(numCol>=nbCol){
res->cnx->error_code=-1;
sprintf_s(res->cnx->error_string,2048,"num Column out of bounds",2048);
sprintf_s(res->cnx->error_string,ERROR_STRING_LENGTH,"num Column out of bounds",ERROR_STRING_LENGTH);
return NULL;
}
indexElmt=(res->numRow-res->first_row)*nbCol+numCol;
Expand All @@ -246,14 +246,14 @@ int fourd_field_to_string(FOURD_RESULT *res,unsigned int numCol,char **value,int
*value=NULL;
*len=0;
res->cnx->error_code=-1;
sprintf_s(res->cnx->error_string,2048,"num Row out of bounds",2048);
sprintf_s(res->cnx->error_string,ERROR_STRING_LENGTH,"num Row out of bounds",ERROR_STRING_LENGTH);
return 0;
}
if(numCol>=nbCol){
*value=NULL;
*len=0;
res->cnx->error_code=-1;
sprintf_s(res->cnx->error_string,2048,"num Column out of bounds",2048);
sprintf_s(res->cnx->error_string,ERROR_STRING_LENGTH,"num Column out of bounds",ERROR_STRING_LENGTH);
return 0;
}
indexElmt=(res->numRow-res->first_row)*nbCol+numCol;
Expand Down Expand Up @@ -453,4 +453,4 @@ const char* fourd_get_statement_preferred_image_types(FOURD_STATEMENT *state)
void fourd_timeout(FOURD* cnx,int timeout)
{
cnx->timeout=timeout;
}
}
2 changes: 1 addition & 1 deletion lib4d_sql/fourd.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ typedef struct in_addr IN_ADDR;
#define BUFFER_LENGTH 131072
#define ERROR_STRING_LENGTH 2048

#define MAX_HEADER_SIZE 2048
#define MAX_HEADER_SIZE 1048576
#define DEFAULT_IMAGE_TYPE "jpg"
#define MAX_LENGTH_COLUMN_NAME 255

Expand Down
38 changes: 19 additions & 19 deletions lib4d_sql/fourd_interne.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,19 @@ int _snprintf(char *buff, int size, const char *format,...)
#endif
int login(FOURD *cnx,unsigned short int id_cnx,const char *user,const char*pwd,const char*image_type)
{
char msg[2048];
char msg[MAX_HEADER_SIZE];
FOURD_RESULT state;
char *user_b64=NULL,*pwd_b64=NULL;
int len;
_clear_atrr_cnx(cnx);
#if __LOGIN_BASE64__
user_b64=base64_encode(user,strlen(user),&len);
pwd_b64=base64_encode(pwd,strlen(pwd),&len);
sprintf_s(msg,2048,"%03d LOGIN \r\nUSER-NAME-BASE64:%s\r\nUSER-PASSWORD-BASE64:%s\r\nPREFERRED-IMAGE-TYPES:%s\r\nREPLY-WITH-BASE64-TEXT:Y\r\nPROTOCOL-VERSION:0.1a\r\n\r\n",id_cnx,user_b64,pwd_b64,image_type);
sprintf_s(msg,MAX_HEADER_SIZE,"%03d LOGIN \r\nUSER-NAME-BASE64:%s\r\nUSER-PASSWORD-BASE64:%s\r\nPREFERRED-IMAGE-TYPES:%s\r\nREPLY-WITH-BASE64-TEXT:Y\r\nPROTOCOL-VERSION:0.1a\r\n\r\n",id_cnx,user_b64,pwd_b64,image_type);
free(user_b64);
free(pwd_b64);
#else
sprintf_s(msg,2048,"%03d LOGIN \r\nUSER-NAME:%s\r\nUSER-PASSWORD:%s\r\nPREFERRED-IMAGE-TYPES:%s\r\nREPLY-WITH-BASE64-TEXT:Y\r\nPROTOCOL-VERSION:0.1a\r\n\r\n",id_cnx,user,pwd,image_type);
sprintf_s(msg,MAX_HEADER_SIZE,"%03d LOGIN \r\nUSER-NAME:%s\r\nUSER-PASSWORD:%s\r\nPREFERRED-IMAGE-TYPES:%s\r\nREPLY-WITH-BASE64-TEXT:Y\r\nPROTOCOL-VERSION:0.1a\r\n\r\n",id_cnx,user,pwd,image_type);
#endif
socket_send(cnx,msg);
if(receiv_check(cnx,&state)!=0)
Expand All @@ -122,10 +122,10 @@ int _query(FOURD *cnx,unsigned short int id_cmd,const char *request,FOURD_RESULT
res=calloc(1,sizeof(FOURD_RESULT));
#if __STATEMENT_BASE64__
request_b64=base64_encode(request,strlen(request),&len);
sprintf_s(msg,2048,"%03d EXECUTE-STATEMENT\r\nFIRST-PAGE-SIZE:999999\r\nSTATEMENT-BASE64:%s\r\nOutput-Mode:%s\r\nPREFERRED-IMAGE-TYPES:%s\r\n\r\n",id_cmd,request_b64,"release",image_type);
sprintf_s(msg,MAX_HEADER_SIZE,"%03d EXECUTE-STATEMENT\r\nFIRST-PAGE-SIZE:999999\r\nSTATEMENT-BASE64:%s\r\nOutput-Mode:%s\r\nPREFERRED-IMAGE-TYPES:%s\r\n\r\n",id_cmd,request_b64,"release",image_type);
free(request_b64);
#else
sprintf_s(msg,2048,"%03d EXECUTE-STATEMENT\r\nFIRST-PAGE-SIZE:999999\r\nSTATEMENT:%s\r\nOutput-Mode:%s\r\nPREFERRED-IMAGE-TYPES:%s\r\n\r\n",id_cmd,request,"release",image_type);
sprintf_s(msg,MAX_HEADER_SIZE,"%03d EXECUTE-STATEMENT\r\nFIRST-PAGE-SIZE:999999\r\nSTATEMENT:%s\r\nOutput-Mode:%s\r\nPREFERRED-IMAGE-TYPES:%s\r\n\r\n",id_cmd,request,"release",image_type);
#endif
cnx->updated_row=-1;
socket_send(cnx,msg);
Expand Down Expand Up @@ -204,7 +204,7 @@ int _query_param(FOURD *cnx,unsigned short int id_cmd, const char *request,unsig
/* construct Header */
#if __STATEMENT_BASE64__
request_b64=base64_encode(request,strlen(request),&len);
sprintf_s(msg,2048,"%03d EXECUTE-STATEMENT\r\nFIRST-PAGE-SIZE:999999\r\nSTATEMENT-BASE64:%s\r\nOutput-Mode:%s\r\nPREFERRED-IMAGE-TYPES:%s\r\nPARAMETER-TYPES:%s\r\n\r\n",id_cmd,request_b64,"release",image_type,sParam);
sprintf_s(msg,MAX_HEADER_SIZE,"%03d EXECUTE-STATEMENT\r\nFIRST-PAGE-SIZE:999999\r\nSTATEMENT-BASE64:%s\r\nOutput-Mode:%s\r\nPREFERRED-IMAGE-TYPES:%s\r\nPARAMETER-TYPES:%s\r\n\r\n",id_cmd,request_b64,"release",image_type,sParam);
free(request_b64);
#else
sprintf_s(msg,MAX_HEADER_SIZE-1,"%03d EXECUTE-STATEMENT\r\nFIRST-PAGE-SIZE:999999\r\nSTATEMENT:%s\r\nOutput-Mode:%s\r\nPREFERRED-IMAGE-TYPES:%s\r\nPARAMETER-TYPES:%s\r\n\r\n",id_cmd,request,"release",image_type,sParam);
Expand Down Expand Up @@ -243,15 +243,15 @@ int _query_param(FOURD *cnx,unsigned short int id_cmd, const char *request,unsig
command_index and statement_id is identify by result of execute statement commande */
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)
{
char msg[2048];
char msg[MAX_HEADER_SIZE];


_clear_atrr_cnx(cnx);

if(result==NULL) {
return 0;
}
sprintf_s(msg,2048,"%03d FETCH-RESULT\r\nSTATEMENT-ID:%d\r\nCOMMAND-INDEX:%03d\r\nFIRST-ROW-INDEX:%d\r\nLAST-ROW-INDEX:%d\r\nOutput-Mode:%s\r\n\r\n",id_cmd,statement_id,command_index,first_row,last_row,"release");
sprintf_s(msg,MAX_HEADER_SIZE,"%03d FETCH-RESULT\r\nSTATEMENT-ID:%d\r\nCOMMAND-INDEX:%03d\r\nFIRST-ROW-INDEX:%d\r\nLAST-ROW-INDEX:%d\r\nOutput-Mode:%s\r\n\r\n",id_cmd,statement_id,command_index,first_row,last_row,"release");
socket_send(cnx,msg);
if(receiv_check(cnx,result)!=0)
return 1;
Expand Down Expand Up @@ -304,15 +304,15 @@ int _fetch_result(FOURD_RESULT *res,unsigned short int id_cmd)
}
int close_statement(FOURD_RESULT *res,unsigned short int id_cmd)
{
char msg[2048];
char msg[MAX_HEADER_SIZE];
FOURD *cnx=NULL;
FOURD_RESULT state;

if(res==NULL)
return 0;
cnx=res->cnx;
_clear_atrr_cnx(cnx);
sprintf_s(msg,2048,"%03d CLOSE-STATEMENT\r\nSTATEMENT-ID:%d\r\n\r\n",id_cmd,res->id_statement);
sprintf_s(msg,MAX_HEADER_SIZE,"%03d CLOSE-STATEMENT\r\nSTATEMENT-ID:%d\r\n\r\n",id_cmd,res->id_statement);
socket_send(cnx,msg);
if(receiv_check(cnx,&state)!=0) {
return 1;
Expand All @@ -322,10 +322,10 @@ int close_statement(FOURD_RESULT *res,unsigned short int id_cmd)
//return 0 if ok 1 if error
int logout(FOURD *cnx,unsigned short int id_cmd)
{
char msg[2048];
char msg[MAX_HEADER_SIZE];
FOURD_RESULT state;
_clear_atrr_cnx(cnx);
sprintf_s(msg,2048,"%03d LOGOUT\r\n\r\n",id_cmd);
sprintf_s(msg,MAX_HEADER_SIZE,"%03d LOGOUT\r\n\r\n",id_cmd);
socket_send(cnx,msg);
if(receiv_check(cnx,&state)!=0) {
return 1;
Expand All @@ -334,10 +334,10 @@ int logout(FOURD *cnx,unsigned short int id_cmd)
}
int quit(FOURD *cnx,unsigned short int id_cmd)
{
char msg[2048];
char msg[MAX_HEADER_SIZE];
FOURD_RESULT state;
_clear_atrr_cnx(cnx);
sprintf_s(msg,2048,"%03d QUIT\r\n\r\n",id_cmd);
sprintf_s(msg,MAX_HEADER_SIZE,"%03d QUIT\r\n\r\n",id_cmd);
socket_send(cnx,msg);
if(receiv_check(cnx,&state)!=0) {
return 1;
Expand Down Expand Up @@ -478,11 +478,11 @@ int traite_header_response(FOURD_RESULT* state)
}
//get Column-Types
{
char column_type[2048];
char column_type[MAX_HEADER_SIZE];
char *column=NULL;
unsigned int num=0;
char *context=NULL;
if(get(header,"Column-Types",column_type,2048)==0) {
if(get(header,"Column-Types",column_type,MAX_HEADER_SIZE)==0) {
Printf("Column-Types => '%s'\n",column_type);
column = strtok_s(column_type, " ",&context);
if(column!=NULL)
Expand All @@ -503,11 +503,11 @@ int traite_header_response(FOURD_RESULT* state)
}
//get Column-Aliases-Base64
{
char column_alias[2048];
char column_alias[MAX_HEADER_SIZE];
char *alias=NULL;
unsigned int num=0;
char *context=NULL;
if(get(header,"Column-Aliases-Base64",column_alias,2048)==0) {
if(get(header,"Column-Aliases-Base64",column_alias,MAX_HEADER_SIZE)==0) {
/* delete the last espace char if exist */
if(column_alias[strlen(column_alias)-1]==' ') {
column_alias[strlen(column_alias)-1]=0;
Expand Down Expand Up @@ -931,7 +931,7 @@ int _valid_query(FOURD *cnx,const char *request)
{
if(_is_multi_query(request)){
cnx->error_code=-5001;
sprintf_s(cnx->error_string,2048,"MultiQuery not supported",2048);
sprintf_s(cnx->error_string,ERROR_STRING_LENGTH,"MultiQuery not supported",ERROR_STRING_LENGTH);
return 0;
}
return 1;
Expand Down

0 comments on commit 3b8f37d

Please sign in to comment.