Skip to content

Commit bad4a7e

Browse files
committed
Merge pull request #143 from acv/fix-status-truncation
Fix wdctl status truncation
2 parents 5170fe3 + 6f40d92 commit bad4a7e

File tree

7 files changed

+227
-61
lines changed

7 files changed

+227
-61
lines changed

NEWS

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# $Id$
1+
# vim: set noet sw=4 ts=4 :
22

33
WiFiDog 1.2.1:
44
* Fix build (#127, #128)
@@ -7,7 +7,8 @@ WiFiDog 1.2.1:
77
* Fix several memory leaks and other potential problems uncovered by
88
static analysis
99
* Refactor SSL initialization
10-
* Prevent duplicate TrustedMAC entries (#145)
10+
* Fix a truncation issue around 112 clients in the status page. (#47)
11+
* Prevent duplicate TrustedMAC entries (#145)
1112

1213
WiFiDog 1.2.0:
1314
* Note: this changelog includes snapshots like 20090925, e.g. everything
@@ -108,18 +109,18 @@ WiFiDog 1.1.2:
108109
* Fixed minor issue with wdctl
109110
* Changed the iptables rules priority to allow existing NAT rules to work
110111
* read()s from central server in auth_server_request() are
111-
now timed-out (via select). This is hopefully a bugfix to the
112-
thread-freezing problem.
112+
now timed-out (via select). This is hopefully a bugfix to the
113+
thread-freezing problem.
113114
* Bugfix non-RFC compliant HTTP requests using \n instead of \r\n as line
114-
terminations as per email from [email protected]
115+
terminations as per email from [email protected]
115116
* Firewall: make the default ruleset for validating users = allow all
116-
(except sending SMTP)
117+
(except sending SMTP)
117118

118119
Fixed issue with FAQ
119120

120121
WiFiDog 1.1.1:
121122
* An auth server on port 80 will now work
122-
* Added an FAQ
123+
* Added an FAQ
123124

124125
WiFiDog 1.1.0:
125126
* Changes:

src/Makefile.am

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ wifidog_SOURCES = commandline.c \
2828
ping_thread.c \
2929
safe.c \
3030
httpd_thread.c \
31-
simple_http.c
31+
simple_http.c \
32+
pstring.c
3233

3334
noinst_HEADERS = commandline.h \
3435
common.h \
@@ -47,6 +48,7 @@ noinst_HEADERS = commandline.h \
4748
ping_thread.h \
4849
safe.h \
4950
httpd_thread.h \
50-
simple_http.h
51+
simple_http.h \
52+
pstring.h
5153

5254
wdctl_SOURCES = wdctl.c

src/pstring.c

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/* vim: set et ts=4 sts=4 sw=4 : */
2+
/********************************************************************\
3+
* This program is free software; you can redistribute it and/or *
4+
* modify it under the terms of the GNU General Public License as *
5+
* published by the Free Software Foundation; either version 2 of *
6+
* the License, or (at your option) any later version. *
7+
* *
8+
* This program is distributed in the hope that it will be useful, *
9+
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
11+
* GNU General Public License for more details. *
12+
* *
13+
* You should have received a copy of the GNU General Public License*
14+
* along with this program; if not, contact: *
15+
* *
16+
* Free Software Foundation Voice: +1-617-542-5942 *
17+
* 59 Temple Place - Suite 330 Fax: +1-617-542-2652 *
18+
* Boston, MA 02111-1307, USA [email protected] *
19+
* *
20+
\********************************************************************/
21+
22+
/** @file pstring.h
23+
@brief Simple pascal string like strings
24+
@author Copyright (C) 2015 Alexandre Carmel-Veilleux <[email protected]>
25+
*/
26+
27+
#include <string.h>
28+
#include <stdlib.h>
29+
30+
#include "safe.h"
31+
#include "pstring.h"
32+
#include "common.h"
33+
34+
static void _pstr_grow(pstr_t *);
35+
36+
/**
37+
* Create a new pascal-string like pstr struct and allocate initial buffer.
38+
* @param None.
39+
* @return A pointer to an opaque pstr_t string, think java StringBuilder
40+
*/
41+
pstr_t *
42+
pstr_new(void)
43+
{
44+
pstr_t *new;
45+
46+
new = (pstr_t *)safe_malloc(sizeof(pstr_t));
47+
new->len = 0;
48+
new->size = MAX_BUF;
49+
new->buf = (char *)safe_malloc(MAX_BUF);
50+
51+
return new;
52+
}
53+
54+
/**
55+
* Convert the pstr_t pointer to a char * pointer, freeing the pstr_t in the
56+
* process. Note that the char * is the buffer of the pstr_t and must be freed
57+
* by the called.
58+
* @param pstr A pointer to a pstr_t struct.
59+
* @return A char * pointer
60+
*/
61+
char *
62+
pstr_to_string(pstr_t *pstr)
63+
{
64+
char *ret = pstr->buf;
65+
free(pstr);
66+
return ret;
67+
}
68+
69+
/**
70+
* Grow a pstr_t's buffer by MAX_BUF chars in length.
71+
* Program terminates if realloc() fails.
72+
* @param pstr A pointer to a pstr_t struct.
73+
* @return void
74+
*/
75+
static void
76+
_pstr_grow(pstr_t *pstr)
77+
{
78+
pstr->buf = (char *)safe_realloc((void *)pstr->buf, (pstr->size + MAX_BUF));
79+
pstr->size += MAX_BUF;
80+
}
81+
82+
/**
83+
* Append a char string to pstr_t, allocating more memory if needed.
84+
* If allocation is needed but fails, program terminates.
85+
* @param pstr A pointer to a pstr_t struct.
86+
* @param string A pointer to char string to append.
87+
*/
88+
void
89+
pstr_cat(pstr_t *pstr, const char *string)
90+
{
91+
size_t inlen = strlen(string);
92+
while ((pstr->len + inlen + 1) > pstr->size) {
93+
_pstr_grow(pstr);
94+
}
95+
strncat((pstr->buf + pstr->len), string, (pstr->size - pstr->len - 1));
96+
pstr->len += inlen;
97+
}
98+
99+
/**
100+
* Append a printf-like formatted char string to a pstr_t string.
101+
* If allocation fails, program terminates.
102+
* @param pstr A pointer to a pstr_t struct.
103+
* @param fmt A char string specifying the format.
104+
* @param ... A va_arg list of argument, like for printf/sprintf.
105+
* @return int Number of bytes added.
106+
*/
107+
int
108+
pstr_append_sprintf(pstr_t *pstr, const char *fmt, ...)
109+
{
110+
va_list ap;
111+
char *str;
112+
int retval;
113+
114+
va_start(ap, fmt);
115+
retval = safe_vasprintf(&str, fmt, ap);
116+
va_end(ap);
117+
118+
if (retval >= 0) {
119+
pstr_cat(pstr, str);
120+
free(str);
121+
}
122+
123+
return retval;
124+
}

src/pstring.h

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/* vim: set et ts=4 sts=4 sw=4 : */
2+
/********************************************************************\
3+
* This program is free software; you can redistribute it and/or *
4+
* modify it under the terms of the GNU General Public License as *
5+
* published by the Free Software Foundation; either version 2 of *
6+
* the License, or (at your option) any later version. *
7+
* *
8+
* This program is distributed in the hope that it will be useful, *
9+
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
11+
* GNU General Public License for more details. *
12+
* *
13+
* You should have received a copy of the GNU General Public License*
14+
* along with this program; if not, contact: *
15+
* *
16+
* Free Software Foundation Voice: +1-617-542-5942 *
17+
* 59 Temple Place - Suite 330 Fax: +1-617-542-2652 *
18+
* Boston, MA 02111-1307, USA [email protected] *
19+
* *
20+
\********************************************************************/
21+
22+
/** @file pstring.h
23+
@brief Simple pascal string like strings
24+
@author Copyright (C) 2015 Alexandre Carmel-Veilleux <[email protected]>
25+
*/
26+
27+
#ifndef _PSTRING_H_
28+
#define _PSTRING_H_
29+
30+
#include <stddef.h>
31+
#include <stdarg.h> /* For va_list */
32+
33+
/**
34+
* Structure to represent a pascal-like string.
35+
*/
36+
struct pstr {
37+
char *buf; /**< @brief Buffer used to hold string. Pointer subject to change. */
38+
size_t len; /**< @brief Current length of the string. */
39+
size_t size; /**< @brief Current maximum size of the buffer. */
40+
};
41+
42+
typedef struct pstr pstr_t; /**< @brief pstr_t is a type for a struct pstr. */
43+
44+
pstr_t *pstr_new(void); /**< @brief Create a new pstr */
45+
char * pstr_to_string(pstr_t *); /**< @brief Convert pstr to a char *, freeing pstr. */
46+
void pstr_cat(pstr_t *, const char *); /**< @brief Appends a string to a pstr_t */
47+
int pstr_append_sprintf(pstr_t *, const char *, ...); /**< @brief Appends formatted string to a pstr_t. */
48+
49+
#endif /* defined(_PSTRING_H_) */

src/safe.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* vim: set et ts=4 sts=4 sw=4 : */
12
/********************************************************************\
23
* This program is free software; you can redistribute it and/or *
34
* modify it under the terms of the GNU General Public License as *
@@ -55,6 +56,18 @@ void * safe_malloc (size_t size) {
5556
return (retval);
5657
}
5758

59+
void *
60+
safe_realloc(void *ptr, size_t newsize)
61+
{
62+
void *retval = NULL;
63+
retval = realloc(ptr, newsize);
64+
if (NULL == retval) {
65+
debug(LOG_CRIT, "Failed to realloc buffer to %d bytes of memory: %s. Bailing out", newsize, strerror(errno));
66+
exit(1);
67+
}
68+
return retval;
69+
}
70+
5871
char * safe_strdup(const char *s) {
5972
char * retval = NULL;
6073
if (!s) {

src/safe.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* vim: set et ts=4 sts=4 sw=4 : */
12
/********************************************************************\
23
* This program is free software; you can redistribute it and/or *
34
* modify it under the terms of the GNU General Public License as *
@@ -33,19 +34,22 @@
3334

3435
/** @brief Safe version of malloc
3536
*/
36-
void * safe_malloc (size_t size);
37+
void *safe_malloc(size_t);
38+
39+
/** @brief Safe version of realloc */
40+
void *safe_realloc(void *, size_t);
3741

3842
/* @brief Safe version of strdup
3943
*/
40-
char * safe_strdup(const char *s);
44+
char * safe_strdup(const char *);
4145

4246
/* @brief Safe version of asprintf
4347
*/
44-
int safe_asprintf(char **strp, const char *fmt, ...);
48+
int safe_asprintf(char **, const char *, ...);
4549

4650
/* @brief Safe version of vasprintf
4751
*/
48-
int safe_vasprintf(char **strp, const char *fmt, va_list ap);
52+
int safe_vasprintf(char **, const char *, va_list);
4953

5054
/* @brief Safe version of fork
5155
*/

0 commit comments

Comments
 (0)