Skip to content

Commit a98fafe

Browse files
committed
Sync
1 parent dc4c5d8 commit a98fafe

8 files changed

+62
-55
lines changed

src/example_read.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
/* (C)opyleft 2001-2022 Frank DENIS */
2+
/* (C)opyleft 2001-2022 Frank DENIS <j at pureftpd dot org> */
33

44
#include <stdio.h>
55
#include <stdlib.h>
@@ -22,7 +22,7 @@ int main(void)
2222
goto end;
2323
}
2424
if ((data = puredb_read(&db, retpos, retlen)) != NULL) {
25-
printf("The maching data is : [%s]\n", data);
25+
printf("Matching data: [%s]\n", data);
2626
puredb_read_free(data);
2727
}
2828
end:

src/example_write.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
/* (C)opyleft 2001-2022 Frank DENIS */
2+
/* (C)opyleft 2001-2022 Frank DENIS <j at pureftpd dot org> */
33

44
#include <stdio.h>
55
#include <stdlib.h>

src/puredb_p.h

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
/* (C)opyleft 2001-2022 Frank DENIS */
2+
/* (C)opyleft 2001-2022 Frank DENIS <j at pureftpd dot org> */
33

44
#ifndef __PUREDB_P_H__
55
#define __PUREDB_P_H__ 1
@@ -88,10 +88,5 @@
8888
extern int errno;
8989
#endif
9090

91-
#ifndef HAVE_STRDUP
92-
# define strdup(X) do { char *x; if ((x = malloc(strlen(X) + 1)) != NULL) \
93-
strcpy(x, (X)); } while(0)
94-
#endif
95-
9691
#endif
9792

src/puredb_read.c

+15-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
/* (C)opyleft 2001-2022 Frank DENIS */
2+
/* (C)opyleft 2001-2022 Frank DENIS <j at pureftpd dot org> */
33

44
#include <config.h>
55

@@ -23,20 +23,20 @@ static puredb_u32_t puredb_hash(const char * const msg, size_t len)
2323
static ssize_t safe_read(const int fd, void * const buf_, size_t maxlen)
2424
{
2525
unsigned char *buf = (unsigned char *) buf_;
26-
ssize_t readen;
26+
ssize_t readnb;
2727

2828
do {
29-
while ((readen = read(fd, buf, maxlen)) < (ssize_t) 0 &&
29+
while ((readnb = read(fd, buf, maxlen)) < (ssize_t) 0 &&
3030
errno == EINTR);
31-
if (readen < (ssize_t) 0 || readen > (ssize_t) maxlen) {
32-
return readen;
31+
if (readnb < (ssize_t) 0 || readnb > (ssize_t) maxlen) {
32+
return readnb;
3333
}
34-
if (readen == (ssize_t) 0) {
34+
if (readnb == (ssize_t) 0) {
3535
ret:
3636
return (ssize_t) (buf - (unsigned char *) buf_);
3737
}
38-
maxlen -= readen;
39-
buf += readen;
38+
maxlen -= readnb;
39+
buf += readnb;
4040
} while (maxlen > (ssize_t) 0);
4141
goto ret;
4242
}
@@ -50,6 +50,9 @@ static int read_be_long(const PureDB * const db,
5050

5151
#ifdef USE_MAPPED_IO
5252
if (db->map != NULL) {
53+
if (db->size < 4 || offset > db->size - 4) {
54+
return -1;
55+
}
5356
mapoffset = db->map + offset;
5457
} else
5558
#endif
@@ -105,10 +108,10 @@ int puredb_open(PureDB * const db, const char *dbfile)
105108
if ((db->fd = open(dbfile, O_RDONLY | O_BINARY)) == -1) {
106109
return -1;
107110
}
108-
if (fstat(db->fd, &st) < 0 ||
109-
(db->size = (puredb_u32_t) st.st_size) > (size_t) 0xffffffff ||
110-
db->size < ((size_t) (256U + 1U) * sizeof(puredb_u32_t) +
111-
sizeof PUREDB_VERSION - (size_t) 1U)) {
111+
if (fstat(db->fd, &st) < 0 || st.st_size > (off_t) 0xffffffff ||
112+
(db->size = (puredb_u32_t) st.st_size) <
113+
((size_t) (256U + 1U) * sizeof(puredb_u32_t) +
114+
sizeof PUREDB_VERSION - (size_t) 1U)) {
112115
close(db->fd);
113116

114117
return -2;

src/puredb_read.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
/* (C)opyleft 2001-2022 Frank DENIS */
2+
/* (C)opyleft 2001-2022 Frank DENIS <j at pureftpd dot org> */
33

44
#ifndef __PUREDB_READ_H__
55
#define __PUREDB_READ_H__ 1
@@ -10,7 +10,7 @@
1010
#define PUREDB_LIB_VERSION 1
1111

1212
#ifndef PUREDB_U32_T
13-
# if SHORT_MAX >= 2147483647
13+
# if SHRT_MAX >= 2147483647
1414
typedef unsigned short puredb_u32_t;
1515
# elif INT_MAX >= 2147483647
1616
typedef unsigned int puredb_u32_t;

src/puredb_write.c

+38-29
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,28 @@
11

2-
/* (C)opyleft 2001-2022 Frank DENIS */
2+
/* (C)opyleft 2001-2022 Frank DENIS <j at pureftpd dot org> */
33

44
#include <config.h>
55

66
#include "puredb_p.h"
77
#include "puredb_write.h"
88

9+
#ifndef HAVE_STRDUP
10+
static char *strdup(const char *str)
11+
{
12+
char *newstr;
13+
size_t str_len_1;
14+
15+
if (str == NULL ||
16+
(str_len_1 = strlen(str) + (size_t) 1U) <= (size_t) 0U ||
17+
(newstr = malloc(str_len_1)) == NULL) {
18+
return NULL;
19+
}
20+
memcpy(newstr, str, str_len_1);
21+
22+
return newstr;
23+
}
24+
#endif
25+
926
static puredb_u32_t puredbw_hash(const char * const msg, size_t len)
1027
{
1128
puredb_u32_t j = (puredb_u32_t) 5381U;
@@ -85,8 +102,8 @@ int puredbw_add(PureDBW * const dbw,
85102
hash0->hash1_list = newpnt;
86103
}
87104
dbw->offset_first_data += sizeof(puredb_u32_t) + sizeof(puredb_u32_t);
88-
hash1 = (Hash1 *) ((unsigned char *) hash0->hash1_list +
89-
hash0->hash1_list_size - sizeof(Hash1));
105+
hash1 = (Hash1 *) (void *) ((unsigned char *) hash0->hash1_list +
106+
hash0->hash1_list_size - sizeof(Hash1));
90107
hash1->hash = hash;
91108
hash1->offset_data = dbw->data_offset_counter;
92109
dbw->data_offset_counter += sizeof(puredb_u32_t) + sizeof(puredb_u32_t) +
@@ -125,8 +142,8 @@ int puredbw_add_s(PureDBW * const dbw,
125142

126143
static int hash1_cmp_hook(const void * const a, const void * const b)
127144
{
128-
register puredb_u32_t ha = ((const Hash1 *) a)->hash;
129-
register puredb_u32_t hb = ((const Hash1 *) b)->hash;
145+
puredb_u32_t ha = ((const Hash1 *) a)->hash;
146+
puredb_u32_t hb = ((const Hash1 *) b)->hash;
130147

131148
if (ha < hb) {
132149
return -1;
@@ -145,9 +162,9 @@ static int hash1_cmp_hook(const void * const a, const void * const b)
145162

146163
static int writekeys(PureDBW * const dbw)
147164
{
148-
register int hash_cnt = (int)
165+
int hash_cnt = (int)
149166
(sizeof dbw->hash_table0 / sizeof dbw->hash_table0[0]);
150-
register const Hash0 *hash0 = dbw->hash_table0;
167+
const Hash0 *hash0 = dbw->hash_table0;
151168

152169
puredb_u32_t offset = (puredb_u32_t)
153170
((1U + sizeof dbw->hash_table0 / sizeof dbw->hash_table0[0]) *
@@ -183,8 +200,8 @@ static int writekeys(PureDBW * const dbw)
183200
hash_cnt = (int) (sizeof dbw->hash_table0 / sizeof dbw->hash_table0[0]);
184201
hash0 = dbw->hash_table0;
185202
do {
186-
register Hash1 *hash1 = hash0->hash1_list;
187-
register size_t list_size = hash0->hash1_list_size;
203+
Hash1 *hash1 = hash0->hash1_list;
204+
size_t list_size = hash0->hash1_list_size;
188205

189206
if (hash1 == NULL) {
190207
const puredb_u32_t null_ =
@@ -229,14 +246,12 @@ static int writekeys(PureDBW * const dbw)
229246

230247
static int freestructs(PureDBW * const dbw)
231248
{
232-
register Hash0 *hash0 = dbw->hash_table0;
249+
Hash0 *hash0 = dbw->hash_table0;
233250
int hash0_cnt = (int) (sizeof dbw->hash_table0 / sizeof dbw->hash_table0[0]);
234251

235252
do {
236-
if (hash0->hash1_list != NULL) {
237-
free(hash0->hash1_list);
238-
hash0->hash1_list = NULL;
239-
}
253+
free(hash0->hash1_list);
254+
hash0->hash1_list = NULL;
240255
hash0++;
241256
hash0_cnt--;
242257
} while (hash0_cnt > 0);
@@ -246,13 +261,13 @@ static int freestructs(PureDBW * const dbw)
246261

247262
static int mergefiles(PureDBW * const dbw)
248263
{
249-
size_t readen;
264+
size_t readnb;
250265
char buf[4096];
251266

252267
rewind(dbw->fpdata);
253-
while ((readen = fread(buf, (size_t) 1U, sizeof buf, dbw->fpdata)) >
268+
while ((readnb = fread(buf, (size_t) 1U, sizeof buf, dbw->fpdata)) >
254269
(size_t) 0U) {
255-
if (fwrite(buf, (size_t) 1U, readen, dbw->fpindex) != readen) {
270+
if (fwrite(buf, (size_t) 1U, readnb, dbw->fpindex) != readnb) {
256271
return -1;
257272
}
258273
}
@@ -293,18 +308,12 @@ static void freeall(PureDBW * const dbw)
293308
fclose(dbw->fpdata);
294309
dbw->fpdata = NULL;
295310
}
296-
if (dbw->file_index != NULL) {
297-
free(dbw->file_index);
298-
dbw->file_index = NULL;
299-
}
300-
if (dbw->file_data != NULL) {
301-
free(dbw->file_data);
302-
dbw->file_data = NULL;
303-
}
304-
if (dbw->file_final != NULL) {
305-
free(dbw->file_final);
306-
dbw->file_final = NULL;
307-
}
311+
free(dbw->file_index);
312+
dbw->file_index = NULL;
313+
free(dbw->file_data);
314+
dbw->file_data = NULL;
315+
free(dbw->file_final);
316+
dbw->file_final = NULL;
308317
}
309318

310319
void puredbw_free(PureDBW * const dbw)

src/puredb_write.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
/* (C)opyleft 2001-2022 Frank DENIS */
2+
/* (C)opyleft 2001-2022 Frank DENIS <j at pureftpd dot org> */
33

44
#ifndef __PUREDB_WRITE_H__
55
#define __PUREDB_WRITE_H__ 1
@@ -10,7 +10,7 @@
1010
#define PUREDBW_LIB_VERSION 1
1111

1212
#ifndef PUREDB_U32_T
13-
# if SHORT_MAX >= 2147483647
13+
# if SHRT_MAX >= 2147483647
1414
typedef unsigned short puredb_u32_t;
1515
# elif INT_MAX >= 2147483647
1616
typedef unsigned int puredb_u32_t;

src/regression.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
/* (C)opyleft 2001-2022 Frank DENIS */
2+
/* (C)opyleft 2001-2022 Frank DENIS <j at pureftpd dot org> */
33

44
#include <config.h>
55
#include "puredb_p.h"

0 commit comments

Comments
 (0)