]> git.proxmox.com Git - mirror_frr.git/blobdiff - lib/defun_lex.l
Merge pull request #13649 from donaldsharp/unlock_the_node_or_else
[mirror_frr.git] / lib / defun_lex.l
index 6c0805a4fa9b4542319eaf82476b35d6bae53698..124f864166f0d7127697f897f6169a6fa3200774 100644 (file)
@@ -1,20 +1,7 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
 /*
  * clippy (CLI preparator in python) C pseudo-lexer
  * Copyright (C) 2016-2017  David Lamparter for NetDEF, Inc.
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License as published by the Free
- * Software Foundation; either version 2 of the License, or (at your option)
- * any later version.
- *
- * This program is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
- * more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; see the file COPYING; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
 /* This is just enough of a lexer to make rough sense of a C source file.
@@ -66,6 +53,7 @@ int comment_link;
 char string_end;
 
 char *value;
+static const char *yyfilename;
 
 static void extendbuf(char **what, const char *arg)
 {
@@ -80,6 +68,8 @@ static void extendbuf(char **what, const char *arg)
 }
 #define extend(x) extendbuf(&value, x)
 
+#ifndef __clang_analyzer__
+
 %}
 
 ID             [A-Za-z0-9_]+
@@ -130,8 +120,17 @@ SPECIAL            [(),]
                                        }
                                }
 <rstring>\\\n                  /* ignore */
+<rstring>\n                    {
+                                       fprintf(stderr,
+                                               "%s:%d: string continues past the end of the line\n",
+                                               yyfilename, yylineno);
+                                       free(value);
+                                       value = NULL;
+                                       BEGIN(INITIAL);
+                                       return STRING;
+                               }
 <rstring>\\.                   extend(yytext);
-<rstring>[^\\\"\']+            extend(yytext);
+<rstring>[^\\\"\'\n]+          extend(yytext);
 
 "DEFUN"                                value = strdup(yytext); return DEFUNNY;
 "DEFUN_NOSH"                   value = strdup(yytext); return DEFUNNY;
@@ -140,6 +139,8 @@ SPECIAL             [(),]
 "DEFPY_NOSH"                   value = strdup(yytext); return DEFUNNY;
 "DEFPY_ATTR"                   value = strdup(yytext); return DEFUNNY;
 "DEFPY_HIDDEN"                 value = strdup(yytext); return DEFUNNY;
+"DEFPY_YANG"                   value = strdup(yytext); return DEFUNNY;
+"DEFPY_YANG_NOSH"              value = strdup(yytext); return DEFUNNY;
 "ALIAS"                                value = strdup(yytext); return DEFUNNY;
 "ALIAS_HIDDEN"                 value = strdup(yytext); return DEFUNNY;
 "install_element"              value = strdup(yytext); return INSTALL;
@@ -155,6 +156,8 @@ SPECIAL             [(),]
 
 %%
 
+#endif /* __clang_analyzer__ */
+
 static int yylex_clr(char **retbuf)
 {
        int rv = def_yylex();
@@ -163,7 +166,7 @@ static int yylex_clr(char **retbuf)
        return rv;
 }
 
-static PyObject *get_args(void)
+static PyObject *get_args(const char *filename, int lineno)
 {
        PyObject *pyObj = PyList_New(0);
        PyObject *pyArg = NULL;
@@ -190,6 +193,13 @@ static PyObject *get_args(void)
                        free(tval);
                        continue;
                }
+               if (token == PREPROC) {
+                       free(tval);
+                       Py_DECREF(pyObj);
+                       return PyErr_Format(PyExc_ValueError,
+                                       "%s:%d: cannot process CPP directive within argument list",
+                                       filename, lineno);
+               }
                if (token == SPECIAL) {
                        if (depth == 1 && (tval[0] == ',' || tval[0] == ')')) {
                                if (pyArg)
@@ -207,6 +217,10 @@ static PyObject *get_args(void)
                        if (tval[0] == ')')
                                depth--;
                }
+               if (!tval)
+                       return PyErr_Format(PyExc_ValueError,
+                                       "%s:%d: invalid token in DEFPY parameters",
+                                       filename, lineno);
                if (!pyArg)
                        pyArg = PyList_New(0);
                PyList_Append(pyArg, PyUnicode_FromString(tval));
@@ -231,6 +245,7 @@ PyObject *clippy_parse(PyObject *self, PyObject *args)
        int token;
        yyin = fd;
        value = NULL;
+       yyfilename = filename;
 
        PyObject *pyCont = PyDict_New();
        PyObject *pyObj = PyList_New(0);
@@ -244,7 +259,13 @@ PyObject *clippy_parse(PyObject *self, PyObject *args)
                case DEFUNNY:
                case INSTALL:
                case AUXILIARY:
-                       pyArgs = get_args();
+                       pyArgs = get_args(filename, lineno);
+                       if (!pyArgs) {
+                               free(tval);
+                               Py_DECREF(pyCont);
+                               yyfilename = NULL;
+                               return NULL;
+                       }
                        pyItem = PyDict_New();
                        PyDict_SetItemString(pyItem, "type", PyUnicode_FromString(tval));
                        PyDict_SetItemString(pyItem, "args", pyArgs);
@@ -260,6 +281,7 @@ PyObject *clippy_parse(PyObject *self, PyObject *args)
                        pyItem = PyDict_New();
                        PyDict_SetItemString(pyItem, "type", PyUnicode_FromString("PREPROC"));
                        PyDict_SetItemString(pyItem, "line", PyUnicode_FromString(tval));
+                       lineno--;
                        break;
                }
                if (pyItem) {
@@ -270,5 +292,6 @@ PyObject *clippy_parse(PyObject *self, PyObject *args)
        }
        def_yylex_destroy();
        fclose(fd);
+       yyfilename = NULL;
        return pyCont;
 }