Skip to content

Commit 9f056f8

Browse files
committed
session BUGFIX ignore leading/trailing ws in capabs
Refs CESNET/libyang#308
1 parent 5776d9b commit 9f056f8

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

src/session.c

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <pthread.h>
1919
#include <sys/time.h>
2020
#include <time.h>
21+
#include <ctype.h>
2122
#include <libyang/libyang.h>
2223

2324
#include "session.h"
@@ -761,8 +762,8 @@ static int
761762
parse_cpblts(struct lyxml_elem *xml, const char ***list)
762763
{
763764
struct lyxml_elem *cpblt;
764-
int ver = -1;
765-
int i = 0;
765+
int ver = -1, i = 0;
766+
const char *cpb_start, *cpb_end;
766767

767768
if (list) {
768769
/* get the storage for server's capabilities */
@@ -787,17 +788,28 @@ parse_cpblts(struct lyxml_elem *xml, const char ***list)
787788
continue;
788789
}
789790

791+
/* skip leading/trailing whitespaces */
792+
for (cpb_start = cpblt->content; isspace(cpb_start[0]); ++cpb_start);
793+
for (cpb_end = cpblt->content + strlen(cpblt->content); (cpb_end > cpblt->content) && isspace(cpb_end[-1]); --cpb_end);
794+
if (!cpb_start[0] || (cpb_end == cpblt->content)) {
795+
ERR("Empty capability \"%s\" received.", cpblt->content);
796+
return -1;
797+
}
798+
790799
/* detect NETCONF version */
791-
if (ver < 0 && !strcmp(cpblt->content, "urn:ietf:params:netconf:base:1.0")) {
800+
if (ver < 0 && !strncmp(cpb_start, "urn:ietf:params:netconf:base:1.0", cpb_end - cpb_start)) {
792801
ver = 0;
793-
} else if (ver < 1 && !strcmp(cpblt->content, "urn:ietf:params:netconf:base:1.1")) {
802+
} else if (ver < 1 && !strncmp(cpb_start, "urn:ietf:params:netconf:base:1.1", cpb_end - cpb_start)) {
794803
ver = 1;
795804
}
796805

797806
/* store capabilities */
798807
if (list) {
799-
(*list)[i] = cpblt->content;
800-
cpblt->content = NULL;
808+
(*list)[i] = strndup(cpb_start, cpb_end - cpb_start);
809+
if (!(*list)[i]) {
810+
ERRMEM;
811+
return -1;
812+
}
801813
i++;
802814
}
803815
}

0 commit comments

Comments
 (0)