Skip to content

Commit 07e88c5

Browse files
committed
Refactor: move check_max_len_valid function to existing utils module
Also, type the argument properly. Signed-off-by: Jan Pokorný <jpokorny@redhat.com>
1 parent 5bafbf5 commit 07e88c5

File tree

4 files changed

+25
-17
lines changed

4 files changed

+25
-17
lines changed

src/ticket.c

+6-15
Original file line numberDiff line numberDiff line change
@@ -31,31 +31,22 @@
3131
#else
3232
#include "alt/range2random_glib.h"
3333
#endif
34-
#include "ticket.h"
34+
#include "booth.h"
3535
#include "config.h"
36-
#include "pacemaker.h"
36+
#include "handler.h"
3737
#include "inline-fn.h"
3838
#include "log.h"
39-
#include "booth.h"
39+
#include "manual.h"
40+
#include "pacemaker.h"
4041
#include "raft.h"
41-
#include "handler.h"
4242
#include "request.h"
43-
#include "manual.h"
43+
#include "ticket.h"
44+
#include "utils.h"
4445

4546
#define TK_LINE 256
4647

4748
extern int TIME_RES;
4849

49-
/* Untrusted input, must fit (incl. \0) in a buffer of max chars. */
50-
int check_max_len_valid(const char *s, int max)
51-
{
52-
int i;
53-
for(i=0; i<max; i++)
54-
if (s[i] == 0)
55-
return 1;
56-
return 0;
57-
}
58-
5950
int find_ticket_by_name(struct booth_config *conf_ptr,
6051
const char *ticket, struct ticket_config **found)
6152
{

src/ticket.h

-2
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,6 @@ void update_ticket_state(struct booth_config *conf_ptr,
164164
*/
165165
int setup_ticket(struct booth_config *conf_ptr);
166166

167-
int check_max_len_valid(const char *s, int max);
168-
169167
/**
170168
* @internal
171169
* Pick a ticket structure based on given name

src/utils.c

+8
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@
1111
#include <stdlib.h> /* fprintf */
1212
#include <string.h> /* strlen, strncpy */
1313

14+
int check_max_len_valid(const char *s, size_t max)
15+
{
16+
for (size_t i = 0; i < max; i++)
17+
if (s[i] == '\0')
18+
return 1;
19+
return 0;
20+
}
21+
1422
void safe_copy(char *dest, const char *value, size_t buflen,
1523
const char *description)
1624
{

src/utils.h

+11
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,17 @@
99

1010
#include <stdlib.h> /* size_t */
1111

12+
/**
13+
* @internal
14+
* For an untrusted string, check that it terminates in @p max initial bytes
15+
*
16+
* @param[in] s string at input
17+
* @param[in] max delimits the termination seeking this big initial chunk
18+
*
19+
* @return 1 if early termination satisified, 0 if not
20+
*/
21+
int check_max_len_valid(const char *s, size_t max);
22+
1223
/**
1324
* @internal
1425
* Like strncpy, but with explicit protection and better diagnostics

0 commit comments

Comments
 (0)