Skip to content

Commit 18de719

Browse files
committed
T7557: Use PCRE2
1 parent 8d3275d commit 18de719

6 files changed

Lines changed: 25 additions & 15 deletions

File tree

configure.ac

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ AC_COPYRIGHT([Copyright (c) 2024 VyOS maintainers and contributors.])
44
#AC_PROG_CC
55
AM_PROG_CC_C_O
66

7-
AC_CHECK_HEADER([pcre.h], [], [AC_MSG_FAILURE([pcre.h is not found.])])
87
AC_CHECK_HEADER([libcidr.h], [], [AC_MSG_FAILURE([libcidr.h is not found.])])
98

109
AM_INIT_AUTOMAKE([gnu no-dist-gzip dist-bzip2 subdir-objects])
@@ -14,5 +13,6 @@ AC_CONFIG_FILES([Makefile src/Makefile tests/Makefile man/Makefile])
1413
AC_CONFIG_HEADERS([src/config.h])
1514

1615
PKG_CHECK_MODULES([CHECK], [check >= 0.9.4])
16+
PKG_CHECK_MODULES([PCRE2], [libpcre2-8])
1717

1818
AC_OUTPUT

debian/control

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ Source: ipaddrcheck
22
Section: contrib/net
33
Priority: extra
44
Maintainer: VyOS Package Maintainers <maintainers@vyos.net>
5-
Build-Depends: autoconf, debhelper (>= 9), libpcre3-dev, libcidr-dev, check
5+
Build-Depends: autoconf, debhelper (>= 9), libpcre2-dev, libcidr-dev, check
66
Standards-Version: 3.9.6
77

88
Package: ipaddrcheck
99
Architecture: any
10-
Depends: libpcre3, libcidr0, ${shlibs:Depends}, ${misc:Depends}
10+
Depends: libpcre2-8-0, libcidr0, ${shlibs:Depends}, ${misc:Depends}
1111
Description: IPv4 and IPv6 address validation utility
1212
A validation utility for IPv4 and IPv6 addresses.

src/Makefile.am

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
AM_CFLAGS = --pedantic -Wall -Werror -std=c99 -O2
2-
AM_LDFLAGS =
2+
AM_CPPFLAGS = $(PCRE2_CFLAGS)
33

44
ipaddrcheck_SOURCES = ipaddrcheck.c ipaddrcheck_functions.c
5-
ipaddrcheck_LDADD = -lcidr -lpcre
5+
ipaddrcheck_LDADD = -lcidr $(PCRE2_LIBS)
66

77
bin_PROGRAMS = ipaddrcheck

src/ipaddrcheck_functions.c

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,25 +41,31 @@
4141

4242
int regex_matches(const char* regex, const char* str)
4343
{
44-
int offsets[1];
45-
pcre *re;
44+
pcre2_code *re;
4645
int rc;
47-
const char *error;
48-
int erroffset;
46+
int out;
47+
int error;
48+
PCRE2_SIZE erroffset;
4949

50-
re = pcre_compile(regex, 0, &error, &erroffset, NULL);
50+
re = pcre2_compile((PCRE2_SPTR)regex, PCRE2_ZERO_TERMINATED, 0, &error, &erroffset, NULL);
5151
assert(re != NULL);
5252

53-
rc = pcre_exec(re, NULL, str, strlen(str), 0, 0, offsets, 1);
53+
pcre2_match_data *match = pcre2_match_data_create_from_pattern(re, NULL);
54+
55+
rc = pcre2_match(re, (PCRE2_SPTR)str, strlen(str), 0, 0, match, NULL);
5456

5557
if( rc >= 0)
5658
{
57-
return RESULT_SUCCESS;
59+
out = RESULT_SUCCESS;
5860
}
5961
else
6062
{
61-
return RESULT_FAILURE;
63+
out = RESULT_FAILURE;
6264
}
65+
66+
pcre2_match_data_free(match);
67+
pcre2_code_free(re);
68+
return out;
6369
}
6470

6571

src/ipaddrcheck_functions.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@
2323
#ifndef IPADDRCHECK_FUNCTIONS_H
2424
#define IPADDRCHECK_FUNCTIONS_H
2525

26+
#define PCRE2_CODE_UNIT_WIDTH 8
27+
2628
#include <stdio.h>
2729
#include <stdlib.h>
2830
#include <string.h>
2931
#include <getopt.h>
30-
#include <pcre.h>
32+
#include <pcre2.h>
3133
#include <libcidr.h>
3234

3335
#define INVALID_PROTO -1

tests/Makefile.am

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
AM_CPPFLAGS = $(PCRE2_CFLAGS)
2+
13
TESTS = check_ipaddrcheck integration_tests.sh
24

35
TESTS_ENVIRONMENT = top_srcdir=$(top_srcdir) PATH=.:$(top_srcdir)/src:$$PATH
46

57
check_PROGRAMS = check_ipaddrcheck
68
check_ipaddrcheck_SOURCES = check_ipaddrcheck.c ../src/ipaddrcheck_functions.c
79
check_ipaddrcheck_CFLAGS = @CHECK_CFLAGS@
8-
check_ipaddrcheck_LDADD = -lcidr -lpcre @CHECK_LIBS@
10+
check_ipaddrcheck_LDADD = -lcidr $(PCRE2_LIBS) @CHECK_LIBS@

0 commit comments

Comments
 (0)