clicon / clixon

YANG-based toolchain including NETCONF and RESTCONF interfaces and an interactive CLI
http://www.clicon.org/
Other
215 stars 72 forks source link

Error parsing clixon-config.yang #335

Closed magicalfox007 closed 2 years ago

magicalfox007 commented 2 years ago

Anybody met this error when starting clixon_backend ?

Dec 20 13:20:02: clixon_yang_parseerror: 344: Yang error: /usr/share/clixon/yang/clixon-config@2019-09-11.yang on line 1: syntax error at or before: '' Dec 20 13:20:02: Yang error: /usr/share/clixon/yang/clixon-config@2019-09-11.yang on line 1

developing based on clixon-4.2.0

olofhagsand commented 2 years ago

I never seen that for a YANG file, that parser is fairly static. Also, seems like a very old version. could be a version mis-match of some sort. Line 1 in a (formatted) yang file is also very early, usually just "module clixon-config{\n". One far-fetched explanation would be some escape character in the file, or possible mis-handling of \r from DOS newline formatting.

magicalfox007 commented 2 years ago

It turns out to be size-end problem. As below is code from clixon-4.2.0/lib/clixon_yang.c

yang_stmt* yang_parse_file(int         fd,
                           const char* name,
                           yang_stmt*  ysp)
{
    char*         buf = NULL;
    int           i; 
    int           c;                          // type of c is int
    int           len;
    yang_stmt*    ymod = NULL;
    int           ret;

    len = BUFLEN; /* any number is fine */

    if ((buf = malloc(len)) == NULL)
    {
        perror("pt_file malloc");
        return NULL;
    }

    memset(buf, 0, len);
    i = 0; /* position in buf */

    while (1)  /* read the whole file */
    {
        if ((ret = read(fd, &c, 1)) < 0)    // use c to get char
        {
            clicon_err(OE_XML, errno, "read");
            break;
        }

        if (ret == 0)
        {
            break;    /* eof */
        }

        if (len == i)
        {
            if ((buf = realloc(buf, 2 * len)) == NULL)
            {
                clicon_err(OE_XML, errno, "realloc");
                goto done;
            }

            memset(buf + len, 0, len);
            len *= 2;
        }

        buf[i++] = (char)(c & 0xff);   // buf may fail to get correct value
    } /* read a line */

In latest vesion,int i has been modified to char i

olofhagsand commented 2 years ago

Does this mean this error only occurs in 4.2.0 and has been fixed in later versions, and there is a workaround by the type change patch? If so, can you please close this issue.

magicalfox007 commented 2 years ago

This error only occurs in 4.2.0 or other versions which includes "int c" statement in function "yang_parse_file". (e.g. still exists in 4.3.0) It has been fixed in latest version.