Skip to content

Commit

Permalink
FIXED: nats_GetJWTOrSeed to understand Windows \r\n lites
Browse files Browse the repository at this point in the history
  • Loading branch information
levb committed Sep 30, 2024
1 parent 87d7472 commit 912ebeb
Showing 1 changed file with 51 additions and 46 deletions.
97 changes: 51 additions & 46 deletions src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -2064,18 +2064,16 @@ nats_HostIsIP(const char *host)
}

static bool
_isLineAnHeader(const char *ptr)
_isLineAnHeader(const char *ptr, int len)
{
char *last = NULL;
int len = 0;
int count = 0;
bool done = false;

// We are looking for a header. Based on the Go client's regex,
// the strict requirement is that it ends with at least 3 consecutive
// `-` characters. It must also have 3 consecutive `-` before that.
// So the minimum size would be 6.
len = (int) strlen(ptr);
if (len < 6)
return false;

Expand Down Expand Up @@ -2118,61 +2116,72 @@ _isLineAnHeader(const char *ptr)
return false;
}

// Finds the next entire line in next, sets start to the beginning of the line,
// updates next to point to the remaining bytes, and returns the line's length.
// If there are no more non-empty lines, returns 0.
static inline int
_scan_line(const char **start, const char **next)
{
const char *ptr = *next;
int n = 0;

// skip empty lines in the beginning
while (*ptr == '\r' || *ptr == '\n')
ptr++;

Check warning on line 2130 in src/util.c

View check run for this annotation

Codecov / codecov/patch

src/util.c#L2130

Added line #L2130 was not covered by tests
*start = ptr;

// Consume until we reach the end of the line
while (*ptr != '\r' && *ptr != '\n' && *ptr != '\0')
{
ptr++;
n++;
}

if (n == 0)
return 0;

// skip the subsequent empty lines, 'cause why not?
while (*ptr == '\r' || *ptr == '\n')
ptr++;
*next = ptr;

return n;
}

natsStatus
nats_GetJWTOrSeed(char **val, const char *content, int item)
{
natsStatus s = NATS_OK;
char *pch = NULL;
char *str = NULL;
char *saved = NULL;
int curItem = 0;
int orgLen = 0;
char *nt = NULL;

// First, make a copy of the original content since
// we are going to call strtok on it, which alters it.
str = NATS_STRDUP(content);
if (str == NULL)
return nats_setDefaultError(NATS_NO_MEMORY);

orgLen = (int) strlen(str);
natsStatus s = NATS_OK;
const char *next = content;
const char *line = NULL;
int lineLen = 0;
int curItem = 0;
const char *saved = NULL;
int savedLen = 0;

pch = nats_strtok(str, "\n", &nt);
while (pch != NULL)
while ((lineLen = _scan_line(&line, &next)) > 0)
{
if (_isLineAnHeader(pch))
if (_isLineAnHeader(line, lineLen))
{
// We got the start of the section. Save the next line
// as the possible returned value if the following line
// is a header too.
pch = nats_strtok(NULL, "\n", &nt);
saved = pch;
savedLen = _scan_line(&saved, &next);
if (savedLen == 0)
break; // premature end of file?

while (pch != NULL)
{
pch = nats_strtok(NULL, "\n", &nt);
if (pch == NULL)
break;

// We tolerate empty string(s).
if (*pch == '\0')
continue;

break;
}
if (pch == NULL)
break;

if (_isLineAnHeader(pch))
lineLen = _scan_line(&line, &next);
if (_isLineAnHeader(line, lineLen))
{
// Is this the item we were looking for?
if (curItem == item)
if ((curItem == item) && (savedLen > 0))
{
// Return a copy of the saved line
*val = NATS_STRDUP(saved);
*val = NATS_CALLOC(savedLen + 1, 1);
if (*val == NULL)
s = nats_setDefaultError(NATS_NO_MEMORY);

else
memcpy(*val, saved, savedLen);
break;
}
else if (++curItem > 1)
Expand All @@ -2181,12 +2190,8 @@ nats_GetJWTOrSeed(char **val, const char *content, int item)
}
}
}
pch = nats_strtok(NULL, "\n", &nt);
}

memset(str, 0, orgLen);
NATS_FREE(str);

// Nothing was found, return NATS_NOT_FOUND but don't set the stack error.
if ((s == NATS_OK) && (*val == NULL))
return NATS_NOT_FOUND;
Expand Down

0 comments on commit 912ebeb

Please sign in to comment.