1
0
mirror of https://github.com/fumiama/simple-dict.git synced 2026-06-23 04:30:35 +08:00

null pointer chk

This commit is contained in:
fumiama
2021-02-18 14:32:08 +08:00
parent a1bae57f8c
commit 7b5fcc110d

View File

@@ -7,7 +7,6 @@
#include <arpa/inet.h> #include <arpa/inet.h>
#include <netdb.h> #include <netdb.h>
#include <stdio.h> #include <stdio.h>
#include <errno.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
@@ -79,7 +78,7 @@ int bindServer(uint16_t port, u_int try_times) {
fd = socket(AF_INET, SOCK_STREAM, 0); fd = socket(AF_INET, SOCK_STREAM, 0);
while(!~(result = bind(fd, (struct sockaddr *)&server_addr, struct_len)) && fail_count++ < try_times) sleep(1); while(!~(result = bind(fd, (struct sockaddr *)&server_addr, struct_len)) && fail_count++ < try_times) sleep(1);
if(!~result && fail_count >= try_times) { if(!~result && fail_count >= try_times) {
perror("Bind server failure!"); puts("Bind server failure!");
return 0; return 0;
} else{ } else{
puts("Bind server success!"); puts("Bind server success!");
@@ -92,7 +91,7 @@ int listenSocket(u_int try_times) {
int result = -1; int result = -1;
while(!~(result = listen(fd, 10)) && fail_count++ < try_times) sleep(1); while(!~(result = listen(fd, 10)) && fail_count++ < try_times) sleep(1);
if(!~result && fail_count >= try_times) { if(!~result && fail_count >= try_times) {
perror("Listen failed!"); puts("Listen failed!");
return 0; return 0;
} else{ } else{
puts("Listening...."); puts("Listening....");
@@ -107,8 +106,8 @@ int freeAfterSend(int accept_fd, char *data, size_t length) {
} }
int sendData(int accept_fd, char *data, size_t length) { int sendData(int accept_fd, char *data, size_t length) {
if(send(accept_fd, data, length, 0) < 0) { if(!~send(accept_fd, data, length, 0)) {
perror("Send data error"); puts("Send data error");
return 0; return 0;
} else { } else {
printf("Send data: "); printf("Send data: ");
@@ -122,13 +121,13 @@ int sendAll(int accept_fd, char *buff) {
int re = 1; int re = 1;
FILE *fp = openDict(LOCK_SH); FILE *fp = openDict(LOCK_SH);
size_t numbytes; size_t numbytes;
//rewind(fp); if(fp) {
sprintf(buff, "%zd", fileSize(file_path)); sprintf(buff, "%zd", fileSize(file_path));
re = sendData(accept_fd, buff, strlen(buff)); re = sendData(accept_fd, buff, strlen(buff));
while((numbytes = fread(buff, 1, BUFSIZ, fp)) > 0) { while(re && (numbytes = fread(buff, 1, BUFSIZ, fp)) > 0)
re = sendData(accept_fd, buff, numbytes); re = sendData(accept_fd, buff, numbytes);
closeDict(fp);
} }
closeDict(fp);
return re; return re;
} }
@@ -150,7 +149,7 @@ int s0_init(int *s, int accept_fd, char *buff, size_t numbytes) {
int s1_get(int *s, int accept_fd, char *buff) { int s1_get(int *s, int accept_fd, char *buff) {
FILE *fp = openDict(LOCK_SH); FILE *fp = openDict(LOCK_SH);
DICTBLK dict; DICTBLK dict;
while(fread(&dict, DICTBLKSZ, 1, fp) > 0) { if(fp) while(fread(&dict, DICTBLKSZ, 1, fp) > 0) {
u_char ks = dict.keysize; u_char ks = dict.keysize;
dict.key[ks] = 0; dict.key[ks] = 0;
printf("[%s] Look key: (%d)%s\n", buff, ks, dict.key); printf("[%s] Look key: (%d)%s\n", buff, ks, dict.key);
@@ -171,23 +170,27 @@ int s1_get(int *s, int accept_fd, char *buff) {
int s2_set(int *s, int accept_fd, char *buff, size_t numbytes) { int s2_set(int *s, int accept_fd, char *buff, size_t numbytes) {
FILE *fp = openDict(LOCK_EX); FILE *fp = openDict(LOCK_EX);
//rewind(fp); if(fp) {
*s = 3; *s = 3;
while(fread(&dict, DICTBLKSZ, 1, fp) > 0) { while(fread(&dict, DICTBLKSZ, 1, fp) > 0) {
u_char ks = dict.keysize; u_char ks = dict.keysize;
dict.key[ks] = 0; dict.key[ks] = 0;
printf("[%zd] Key size: %d\n", numbytes, ks); printf("[%zd] Key size: %d\n", numbytes, ks);
if(!dict.keysize || !strcmp(buff, dict.key)) { if(!dict.keysize || !strcmp(buff, dict.key)) {
copyKey(); copyKey();
fseek(fp, -DICTBLKSZ, SEEK_CUR); fseek(fp, -DICTBLKSZ, SEEK_CUR);
fp_cross = fp; fp_cross = fp;
return sendData(accept_fd, "data", 4); return sendData(accept_fd, "data", 4);
}
} }
copyKey();
fseek(fp, 0, SEEK_END);
fp_cross = fp;
return sendData(accept_fd, "data", 4);
} else {
*s = 0;
return sendData(accept_fd, "erro", 4);
} }
copyKey();
fseek(fp, 0, SEEK_END);
fp_cross = fp;
return sendData(accept_fd, "data", 4);
} }
int s3_setData(int *s, int accept_fd, char *buff, size_t numbytes) { int s3_setData(int *s, int accept_fd, char *buff, size_t numbytes) {
@@ -212,7 +215,7 @@ int s4_del(int *s, int accept_fd, char *buff) {
FILE *fp = openDict(LOCK_EX); FILE *fp = openDict(LOCK_EX);
DICTBLK dict; DICTBLK dict;
*s = 0; *s = 0;
while(fread(&dict, DICTBLKSZ, 1, fp) > 0) { if(fp) while(fread(&dict, DICTBLKSZ, 1, fp) > 0) {
dict.key[dict.keysize] = 0; dict.key[dict.keysize] = 0;
if(!strcmp(buff, dict.key)) { if(!strcmp(buff, dict.key)) {
fseek(fp, -DICTBLKSZ+(DATASIZE-1), SEEK_CUR); fseek(fp, -DICTBLKSZ+(DATASIZE-1), SEEK_CUR);
@@ -235,9 +238,8 @@ int s5_list(int *s, int accept_fd, char *buff) {
off_t size = fileSize(file_path) / DICTBLKSZ; off_t size = fileSize(file_path) / DICTBLKSZ;
char *keys = calloc(size, DATASIZE); char *keys = calloc(size, DATASIZE);
DICTBLK dict; DICTBLK dict;
if(keys) { FILE *fp = openDict(LOCK_SH);
FILE *fp = openDict(LOCK_SH); if(keys && fp) {
//rewind(fp);
keys[0] = 0; keys[0] = 0;
while(fread(&dict, DICTBLKSZ, 1, fp) > 0) { while(fread(&dict, DICTBLKSZ, 1, fp) > 0) {
u_char ks = dict.keysize; u_char ks = dict.keysize;
@@ -298,7 +300,7 @@ void handleAccept(void *p) {
puts("Connected to the client."); puts("Connected to the client.");
signal(SIGQUIT, handle_quit); signal(SIGQUIT, handle_quit);
pthread_t thread; pthread_t thread;
if (pthread_create(&thread, NULL, (void *)&acceptTimer, p)) perror("Error creating timer thread"); if (pthread_create(&thread, NULL, (void *)&acceptTimer, p)) puts("Error creating timer thread");
else puts("Creating timer thread succeeded"); else puts("Creating timer thread succeeded");
sendData(accept_fd, "Welcome to simple dict server.", 31); sendData(accept_fd, "Welcome to simple dict server.", 31);
int s = -1; int s = -1;
@@ -314,9 +316,9 @@ void handleAccept(void *p) {
if(!checkBuffer(accept_fd, &s, buff, numbytes)) break; if(!checkBuffer(accept_fd, &s, buff, numbytes)) break;
} }
printf("Recv %zd bytes\n", numbytes); printf("Recv %zd bytes\n", numbytes);
} else perror("Error allocating buffer"); } else puts("Error allocating buffer");
close(accept_fd); close(accept_fd);
} else perror("Error accepting client"); } else puts("Error accepting client");
} }
void acceptClient() { void acceptClient() {
@@ -327,13 +329,15 @@ void acceptClient() {
if(p < 8) { if(p < 8) {
printf("Run on thread No.%d\n", p); printf("Run on thread No.%d\n", p);
THREADTIMER *timer = malloc(sizeof(THREADTIMER)); THREADTIMER *timer = malloc(sizeof(THREADTIMER));
struct sockaddr_in client_addr; if(timer) {
timer->accept_fd = accept(fd, (struct sockaddr *)&client_addr, &struct_len); struct sockaddr_in client_addr;
timer->thread = &accept_threads[p]; timer->accept_fd = accept(fd, (struct sockaddr *)&client_addr, &struct_len);
timer->touch = time(NULL); timer->thread = &accept_threads[p];
timer->data = NULL; timer->touch = time(NULL);
if (pthread_create(timer->thread, NULL, (void *)&handleAccept, timer)) perror("Error creating thread"); timer->data = NULL;
else puts("Creating thread succeeded"); if (pthread_create(timer->thread, NULL, (void *)&handleAccept, timer)) puts("Error creating thread");
else puts("Creating thread succeeded");
} else puts("Allocate timer error");
} else { } else {
puts("Max thread cnt exceeded"); puts("Max thread cnt exceeded");
sleep(1); sleep(1);
@@ -344,8 +348,10 @@ void acceptClient() {
FILE *openDict(int lock_type) { FILE *openDict(int lock_type) {
FILE *fp = NULL; FILE *fp = NULL;
fp = fopen(file_path, "rb+"); fp = fopen(file_path, "rb+");
if(fp) flock(fileno(fp), lock_type); if(fp) {
printf("Open dict in mode %d\n", lock_type); flock(fileno(fp), lock_type);
printf("Open dict in mode %d\n", lock_type);
} else puts("Open dict error");
return fp; return fp;
} }
@@ -374,7 +380,7 @@ int main(int argc, char *argv[]) {
fclose(fp); fclose(fp);
if(bindServer(port, times)) if(listenSocket(times)) acceptClient(); if(bindServer(port, times)) if(listenSocket(times)) acceptClient();
} else fprintf(stderr, "Error opening dict file: %s\n", argv[as_daemon?4:3]); } else fprintf(stderr, "Error opening dict file: %s\n", argv[as_daemon?4:3]);
} else perror("Start daemon error"); } else puts("Start daemon error");
} else fprintf(stderr, "Error times: %d\n", times); } else fprintf(stderr, "Error times: %d\n", times);
} else fprintf(stderr, "Error port: %d\n", port); } else fprintf(stderr, "Error port: %d\n", port);
} }