Skip to content

Commit 7212f40

Browse files
authored
Merge pull request uriparser#273 from uriparser/issue-264-c99
Start requiring a C99 compiler (fixes uriparser#264)
2 parents c1ff017 + 11725f2 commit 7212f40

21 files changed

Lines changed: 439 additions & 534 deletions

.github/workflows/build-and-test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ jobs:
116116
-Wextra
117117
-pedantic
118118
)
119-
CFLAGS="${compile_flags[*]} -std=c89"
119+
CFLAGS="${compile_flags[*]} -std=c99"
120120
CXXFLAGS="${compile_flags[*]} -std=c++98"
121121
LDFLAGS='-g ${{ matrix.ldflags }}'
122122
cmake_args=(

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ project(uriparser
4242
LANGUAGES
4343
C
4444
)
45-
set(CMAKE_C_STANDARD 90) # same as C89, value "89" not supported by CMake
45+
set(CMAKE_C_STANDARD 99)
4646
set(CMAKE_C_STANDARD_REQUIRED ON)
47-
set(CMAKE_C_EXTENSIONS OFF) # i.e. -std=c90 rather than default -std=gnu90
47+
set(CMAKE_C_EXTENSIONS OFF) # i.e. -std=c99 rather than default -std=gnu99
4848

4949
# See https://verbump.de/ for what these numbers do
5050
set(URIPARSER_SO_CURRENT 2)

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
uriparser is a
88
strictly [RFC 3986](https://datatracker.ietf.org/doc/html/rfc3986) compliant
99
URI parsing and handling library
10-
written in C89 ("ANSI C").
10+
written in C99.
1111
uriparser is cross-platform,
1212
fast,
1313
supports both `char` and `wchar_t`, and

src/UriCommon.c

Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -671,35 +671,30 @@ static UriBool URI_FUNC(PrependNewDotSegment)(URI_TYPE(Uri) * uri,
671671
assert(uri != NULL);
672672
assert(memory != NULL);
673673

674-
{
675-
URI_TYPE(PathSegment) * const segment =
676-
memory->malloc(memory, 1 * sizeof(URI_TYPE(PathSegment)));
674+
URI_TYPE(PathSegment) * const segment =
675+
memory->malloc(memory, 1 * sizeof(URI_TYPE(PathSegment)));
677676

678-
if (segment == NULL) {
679-
return URI_FALSE; /* i.e. raise malloc error */
680-
}
677+
if (segment == NULL) {
678+
return URI_FALSE; /* i.e. raise malloc error */
679+
}
681680

682-
segment->next = uri->pathHead;
681+
segment->next = uri->pathHead;
683682

684-
{
685-
URI_TYPE(TextRange) dotRange;
686-
dotRange.first = URI_FUNC(ConstPwd);
687-
dotRange.afterLast = URI_FUNC(ConstPwd) + 1;
683+
URI_TYPE(TextRange) dotRange;
684+
dotRange.first = URI_FUNC(ConstPwd);
685+
dotRange.afterLast = URI_FUNC(ConstPwd) + 1;
688686

689-
if (uri->owner == URI_TRUE) {
690-
if (URI_FUNC(CopyRange)(&(segment->text), &dotRange, memory)
691-
== URI_FALSE) {
692-
memory->free(memory, segment);
693-
return URI_FALSE; /* i.e. raise malloc error */
694-
}
695-
} else {
696-
segment->text = dotRange; /* copies all members */
697-
}
687+
if (uri->owner == URI_TRUE) {
688+
if (URI_FUNC(CopyRange)(&(segment->text), &dotRange, memory) == URI_FALSE) {
689+
memory->free(memory, segment);
690+
return URI_FALSE; /* i.e. raise malloc error */
698691
}
699-
700-
uri->pathHead = segment;
692+
} else {
693+
segment->text = dotRange; /* copies all members */
701694
}
702695

696+
uri->pathHead = segment;
697+
703698
return URI_TRUE;
704699
}
705700

@@ -730,23 +725,21 @@ UriBool URI_FUNC(FixPathNoScheme)(URI_TYPE(Uri) * uri, UriMemoryManager * memory
730725
}
731726

732727
/* Check for troublesome first path segment containing a colon */
733-
{
734-
UriBool colonFound = URI_FALSE;
735-
const URI_CHAR * walker = uri->pathHead->text.first;
736-
737-
while (walker < uri->pathHead->text.afterLast) {
738-
if (walker[0] == _UT(':')) {
739-
colonFound = URI_TRUE;
740-
break;
741-
}
742-
walker++;
728+
UriBool colonFound = URI_FALSE;
729+
const URI_CHAR * walker = uri->pathHead->text.first;
730+
731+
while (walker < uri->pathHead->text.afterLast) {
732+
if (walker[0] == _UT(':')) {
733+
colonFound = URI_TRUE;
734+
break;
743735
}
736+
walker++;
737+
}
744738

745-
assert((walker == uri->pathHead->text.afterLast) || (colonFound == URI_TRUE));
739+
assert((walker == uri->pathHead->text.afterLast) || (colonFound == URI_TRUE));
746740

747-
if (colonFound == URI_FALSE) {
748-
return URI_TRUE; /* i.e. nothing to do */
749-
}
741+
if (colonFound == URI_FALSE) {
742+
return URI_TRUE; /* i.e. nothing to do */
750743
}
751744

752745
/* Insert "." segment in front */

src/UriFile.c

Lines changed: 44 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -140,57 +140,52 @@ static URI_INLINE int URI_FUNC(UriStringToFilename)(const URI_CHAR * uriString,
140140
return URI_ERROR_NULL;
141141
}
142142

143-
{
144-
const UriBool file_unknown_slashes =
145-
URI_STRNCMP(uriString, _UT("file:"), URI_STRLEN(_UT("file:"))) == 0;
146-
const UriBool file_one_or_more_slashes =
147-
file_unknown_slashes
148-
&& (URI_STRNCMP(uriString, _UT("file:/"), URI_STRLEN(_UT("file:/"))) == 0);
149-
const UriBool file_two_or_more_slashes =
150-
file_one_or_more_slashes
151-
&& (URI_STRNCMP(uriString, _UT("file://"), URI_STRLEN(_UT("file://"))) == 0);
152-
const UriBool file_three_or_more_slashes =
153-
file_two_or_more_slashes
154-
&& (URI_STRNCMP(uriString, _UT("file:///"), URI_STRLEN(_UT("file:///")))
155-
== 0);
156-
157-
const size_t charsToSkip =
158-
file_two_or_more_slashes
159-
? file_three_or_more_slashes
160-
? toUnix
161-
/* file:///bin/bash */
162-
? URI_STRLEN(_UT("file://"))
163-
/* file:///E:/Documents%20and%20Settings */
164-
: URI_STRLEN(_UT("file:///"))
165-
/* file://Server01/Letter.txt */
166-
: URI_STRLEN(_UT("file://"))
167-
: ((file_one_or_more_slashes && toUnix)
168-
/* file:/bin/bash */
169-
/* https://tools.ietf.org/html/rfc8089#appendix-B */
170-
? URI_STRLEN(_UT("file:"))
171-
: ((!toUnix && file_unknown_slashes && !file_one_or_more_slashes)
172-
/* file:c:/path/to/file */
173-
/* https://tools.ietf.org/html/rfc8089#appendix-E.2 */
174-
? URI_STRLEN(_UT("file:"))
175-
: 0));
176-
const size_t charsToCopy = URI_STRLEN(uriString + charsToSkip) + 1;
177-
178-
const UriBool is_windows_network_with_authority = (toUnix == URI_FALSE)
179-
&& file_two_or_more_slashes
180-
&& !file_three_or_more_slashes;
181-
182-
URI_CHAR * const unescape_target =
183-
is_windows_network_with_authority ? (filename + 2) : filename;
184-
185-
if (is_windows_network_with_authority) {
186-
filename[0] = '\\';
187-
filename[1] = '\\';
188-
}
189-
190-
memcpy(unescape_target, uriString + charsToSkip, charsToCopy * sizeof(URI_CHAR));
191-
URI_FUNC(UnescapeInPlaceEx)(filename, URI_FALSE, URI_BR_DONT_TOUCH);
143+
const UriBool file_unknown_slashes =
144+
URI_STRNCMP(uriString, _UT("file:"), URI_STRLEN(_UT("file:"))) == 0;
145+
const UriBool file_one_or_more_slashes =
146+
file_unknown_slashes
147+
&& (URI_STRNCMP(uriString, _UT("file:/"), URI_STRLEN(_UT("file:/"))) == 0);
148+
const UriBool file_two_or_more_slashes =
149+
file_one_or_more_slashes
150+
&& (URI_STRNCMP(uriString, _UT("file://"), URI_STRLEN(_UT("file://"))) == 0);
151+
const UriBool file_three_or_more_slashes =
152+
file_two_or_more_slashes
153+
&& (URI_STRNCMP(uriString, _UT("file:///"), URI_STRLEN(_UT("file:///"))) == 0);
154+
155+
const size_t charsToSkip =
156+
file_two_or_more_slashes
157+
? file_three_or_more_slashes ? toUnix
158+
/* file:///bin/bash */
159+
? URI_STRLEN(_UT("file://"))
160+
/* file:///E:/Documents%20and%20Settings */
161+
: URI_STRLEN(_UT("file:///"))
162+
/* file://Server01/Letter.txt */
163+
: URI_STRLEN(_UT("file://"))
164+
: ((file_one_or_more_slashes && toUnix)
165+
/* file:/bin/bash */
166+
/* https://tools.ietf.org/html/rfc8089#appendix-B */
167+
? URI_STRLEN(_UT("file:"))
168+
: ((!toUnix && file_unknown_slashes && !file_one_or_more_slashes)
169+
/* file:c:/path/to/file */
170+
/* https://tools.ietf.org/html/rfc8089#appendix-E.2 */
171+
? URI_STRLEN(_UT("file:"))
172+
: 0));
173+
const size_t charsToCopy = URI_STRLEN(uriString + charsToSkip) + 1;
174+
175+
const UriBool is_windows_network_with_authority =
176+
(toUnix == URI_FALSE) && file_two_or_more_slashes && !file_three_or_more_slashes;
177+
178+
URI_CHAR * const unescape_target =
179+
is_windows_network_with_authority ? (filename + 2) : filename;
180+
181+
if (is_windows_network_with_authority) {
182+
filename[0] = '\\';
183+
filename[1] = '\\';
192184
}
193185

186+
memcpy(unescape_target, uriString + charsToSkip, charsToCopy * sizeof(URI_CHAR));
187+
URI_FUNC(UnescapeInPlaceEx)(filename, URI_FALSE, URI_BR_DONT_TOUCH);
188+
194189
/* Convert forward slashes to backslashes */
195190
if (!toUnix) {
196191
URI_CHAR * walker = filename;

src/UriMemory.c

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -436,16 +436,14 @@ int uriTestMemoryManagerEx(UriMemoryManager * memory, UriBool challengeAlignment
436436
ptr[2] = 2.2L;
437437
ptr[3] = 3.3L;
438438

439-
{
440-
long double * const ptrNew =
441-
memory->realloc(memory, ptr, 8 * sizeof(long double));
442-
if (ptrNew != NULL) {
443-
ptr = ptrNew;
444-
ptr[4] = 4.4L;
445-
ptr[5] = 5.5L;
446-
ptr[6] = 6.6L;
447-
ptr[7] = 7.7L;
448-
}
439+
long double * const ptrNew =
440+
memory->realloc(memory, ptr, 8 * sizeof(long double));
441+
if (ptrNew != NULL) {
442+
ptr = ptrNew;
443+
ptr[4] = 4.4L;
444+
ptr[5] = 5.5L;
445+
ptr[6] = 6.6L;
446+
ptr[7] = 7.7L;
449447
}
450448

451449
memory->free(memory, ptr);

src/UriNormalize.c

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -524,24 +524,22 @@ static const URI_CHAR * URI_FUNC(PastLeadingZeros)(const URI_CHAR * first,
524524
assert(afterLast != NULL);
525525
assert(first != afterLast);
526526

527-
{
528-
/* Find the first non-zero character */
529-
const URI_CHAR * remainderFirst = first;
530-
while ((remainderFirst < afterLast) && (remainderFirst[0] == _UT('0'))) {
531-
remainderFirst++;
532-
}
533-
534-
/* Is the string /all/ zeros? */
535-
if (remainderFirst == afterLast) {
536-
/* Yes, and length is >=1 because we ruled out the empty string earlier;
537-
* pull back onto rightmost zero */
538-
assert(remainderFirst > first);
539-
remainderFirst--;
540-
assert(remainderFirst[0] == _UT('0'));
541-
}
527+
/* Find the first non-zero character */
528+
const URI_CHAR * remainderFirst = first;
529+
while ((remainderFirst < afterLast) && (remainderFirst[0] == _UT('0'))) {
530+
remainderFirst++;
531+
}
542532

543-
return remainderFirst;
533+
/* Is the string /all/ zeros? */
534+
if (remainderFirst == afterLast) {
535+
/* Yes, and length is >=1 because we ruled out the empty string earlier;
536+
* pull back onto rightmost zero */
537+
assert(remainderFirst > first);
538+
remainderFirst--;
539+
assert(remainderFirst[0] == _UT('0'));
544540
}
541+
542+
return remainderFirst;
545543
}
546544

547545
static void URI_FUNC(DropLeadingZerosInplace)(URI_CHAR * first,
@@ -554,16 +552,13 @@ static void URI_FUNC(DropLeadingZerosInplace)(URI_CHAR * first,
554552
return;
555553
}
556554

557-
{
558-
const URI_CHAR * const remainderFirst =
559-
URI_FUNC(PastLeadingZeros)(first, *afterLast);
555+
const URI_CHAR * const remainderFirst = URI_FUNC(PastLeadingZeros)(first, *afterLast);
560556

561-
if (remainderFirst > first) {
562-
const size_t remainderLen = *afterLast - remainderFirst;
563-
memmove(first, remainderFirst, remainderLen * sizeof(URI_CHAR));
564-
first[remainderLen] = _UT('\0');
565-
*afterLast = first + remainderLen;
566-
}
557+
if (remainderFirst > first) {
558+
const size_t remainderLen = *afterLast - remainderFirst;
559+
memmove(first, remainderFirst, remainderLen * sizeof(URI_CHAR));
560+
first[remainderLen] = _UT('\0');
561+
*afterLast = first + remainderLen;
567562
}
568563
}
569564

@@ -577,13 +572,10 @@ static void URI_FUNC(AdvancePastLeadingZeros)(const URI_CHAR ** first,
577572
return;
578573
}
579574

580-
{
581-
const URI_CHAR * const remainderFirst =
582-
URI_FUNC(PastLeadingZeros)(*first, afterLast);
575+
const URI_CHAR * const remainderFirst = URI_FUNC(PastLeadingZeros)(*first, afterLast);
583576

584-
/* Cut off leading zeros */
585-
*first = remainderFirst;
586-
}
577+
/* Cut off leading zeros */
578+
*first = remainderFirst;
587579
}
588580

589581
static URI_INLINE int URI_FUNC(NormalizeSyntaxEngine)(URI_TYPE(Uri) * uri,

src/UriParse.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1361,14 +1361,12 @@ static const URI_CHAR * URI_FUNC(ParseOwnPortUserInfo)(URI_TYPE(ParserState) * s
13611361

13621362
case _UT('%'):
13631363
state->uri->portText.first = NULL; /* Not a port, reset */
1364-
{
1365-
const URI_CHAR * const afterPct =
1366-
URI_FUNC(ParsePctEncoded)(state, first, afterLast, memory);
1367-
if (afterPct == NULL) {
1368-
return NULL;
1369-
}
1370-
return URI_FUNC(ParseOwnUserInfo)(state, afterPct, afterLast, memory);
1364+
const URI_CHAR * const afterPct =
1365+
URI_FUNC(ParsePctEncoded)(state, first, afterLast, memory);
1366+
if (afterPct == NULL) {
1367+
return NULL;
13711368
}
1369+
return URI_FUNC(ParseOwnUserInfo)(state, afterPct, afterLast, memory);
13721370

13731371
case _UT('@'):
13741372
state->uri->hostText.afterLast = NULL; /* Not a host, reset */

0 commit comments

Comments
 (0)