On 5/16/17 1:32 AM, Simon Horman wrote: >> diff --git a/lib/mpls_pton.c b/lib/mpls_pton.c >> index bd448cfcf14a..6d2e6a69436a 100644 >> --- a/lib/mpls_pton.c >> +++ b/lib/mpls_pton.c >> @@ -7,12 +7,13 @@ >> #include "utils.h" >> >> >> -static int mpls_pton1(const char *name, struct mpls_label *addr) >> +static int mpls_pton1(const char *name, struct mpls_label *addr, >> + unsigned int maxlabels) >> { >> char *endp; >> unsigned count; >> >> - for (count = 0; count < MPLS_MAX_LABELS; count++) { >> + for (count = 0; count < maxlabels; count++) { >> unsigned long label; >> >> label = strtoul(name, &endp, 0); >> @@ -37,17 +38,19 @@ static int mpls_pton1(const char *name, struct >> mpls_label *addr) >> addr += 1; >> } >> /* The address was too long */ >> + fprintf(stderr, "Error: too many labels.\n"); >> return 0; >> } >> >> -int mpls_pton(int af, const char *src, void *addr) >> +int mpls_pton(int af, const char *src, void *addr, size_t alen) >> { >> + unsigned int maxlabels = alen / sizeof(struct mpls_label); > > Could ARRAY_SIZE be used? > > Also, I know its only calculated twice, but might it be nicer to > provide a function or macro to do so? It seems like an ugly thing > to open code.
I did it this way with intention -- to mpls_pton addr is just a buffer of a given length. mpls_pton converts the alen into the number of labels that the buffer can hold and passes that max to mpls_pton1 to do the heavy lifting.