1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Copyright (C) 2018-2022 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include <config.h>

#include <cc/data.h>
#include <exceptions/exceptions.h>
#include <netconf/netconf_log.h>
#include <netconf/netconf_parser.h>
#include <netconf/parser_context.h>

#include <fstream><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <limits><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <sstream><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

namespace isc {
namespace netconf {

ParserContext::ParserContext()
  : sfile_(0), ctx_(NO_KEYWORDS), trace_scanning_(false), trace_parsing_(false)
{
}

ParserContext::~ParserContext()
{
}

isc::data::ElementPtr
ParserContext::parseString(string const& str, ParserType parser_type)
{
    scanStringBegin(str, parser_type);
    return (parseCommon());
}

isc::data::ElementPtr
ParserContext::parseFile(string const& filename, ParserType parser_type) {
    FILE* f = fopen(filename.c_str(), "r");
    if (!f) {
        isc_throw(ParseError, "Unable to open file " << filename);
    }
    scanFileBegin(f, filename, parser_type);
    return (parseCommon());
}

isc::data::ElementPtr
ParserContext::parseCommon() {
    isc::netconf::NetconfParser parser(*this);
    // Uncomment this to get detailed parser logs.
    // trace_parsing_ = true;
    parser.set_debug_level(trace_parsing_);
    try {
        int res = parser.parse();
        if (res != 0) {
            isc_throw(ParseError, "Parser abort");
        }
        scanEnd();
    }
    catch (...) {
        scanEnd();
        throw;
    }
    if (stack_.size() == 1) {
        return (stack_[0]);
    } else {
        isc_throw(ParseError, "Expected exactly one terminal Element expected, found "
                  << stack_.size());
    }
}

void
ParserContext::error(const isc::netconf::location& loc,
                     string const& what,
                     size_t pos)
{
    if (pos == 0) {
        isc_throw(ParseError, loc << ": " << what);
    } else {
        isc_throw(ParseError, loc << " (near " << pos << "): " << what);
    }
}

void
ParserContext::error(string const& what)
{
    isc_throw(ParseError, what);
}

void
ParserContext::fatal(string const& what)
{
    isc_throw(ParseError, what);
}

isc::data::Element::Position
ParserContext::loc2pos(isc::netconf::location& loc)<--- Parameter 'loc' can be declared as reference to const
{
    string const& file = *loc.begin.filename;
    const uint32_t line = loc.begin.line;
    const uint32_t pos = loc.begin.column;
    return (isc::data::Element::Position(file, line, pos));
}

void
ParserContext::require(string const& name,
                       isc::data::Element::Position open_loc,<--- Function parameter 'open_loc' should be passed by const reference.
                       isc::data::Element::Position close_loc)<--- Function parameter 'close_loc' should be passed by const reference.
{
    ConstElementPtr value = stack_.back()->get(name);
    if (!value) {
        isc_throw(ParseError,
                  "missing parameter '" << name << "' ("
                  << stack_.back()->getPosition() << ") ["
                  << contextName() << " map between "
                  << open_loc << " and " << close_loc << "]");
    }
}

void
ParserContext::unique(string const& name,
                      isc::data::Element::Position loc)<--- Function parameter 'loc' should be passed by const reference.
{
    ConstElementPtr value = stack_.back()->get(name);
    if (value) {
        if (ctx_ != NO_KEYWORDS) {
            isc_throw(ParseError, loc << ": duplicate " << name
                      << " entries in " << contextName()
                      << " map (previous at " << value->getPosition() << ")");
        } else {
            isc_throw(ParseError, loc << ": duplicate " << name
                      << " entries in JSON"
                      << " map (previous at " << value->getPosition() << ")");
        }
    }
}

void
ParserContext::enter(const LexerContext& ctx)
{
    cstack_.push_back(ctx_);
    ctx_ = ctx;
}

void
ParserContext::leave()
{
    if (cstack_.empty()) {<--- Assuming that condition 'cstack_.empty()' is not redundant<--- Assuming that condition 'cstack_.empty()' is not redundant
        fatal("unbalanced syntactic context");
    }
    ctx_ = cstack_.back();<--- Access out of bounds
    cstack_.pop_back();<--- Access out of bounds
}

string const
ParserContext::contextName()
{
    switch (ctx_) {
    case NO_KEYWORDS:
        return ("__no keywords__");
    case CONFIG:
        return ("toplevel");
    case NETCONF:
        return ("Netconf");
    case MANAGED_SERVERS:
        return ("managed-servers");
    case SERVER:
        return ("managed-servers entry");
    case CONTROL_SOCKET:
        return ("control-socket");
    case SOCKET_TYPE:
        return ("socket-type");
    case HOOKS_LIBRARIES:
        return ("hooks-libraries");
    case LOGGERS:
        return ("loggers");
    case OUTPUT_OPTIONS:
        return ("output-options");
    default:
        return ("__unknown__");
    }
}

void
ParserContext::warning(const isc::netconf::location& loc,
                       string const& what) {
    ostringstream msg;
    msg << loc << ": " << what;
    LOG_WARN(netconf_logger, NETCONF_CONFIG_SYNTAX_WARNING)
        .arg(msg.str());
}

void
ParserContext::warnAboutExtraCommas(const isc::netconf::location& loc) {
    warning(loc, "Extraneous comma. A piece of configuration may have been omitted.");
}

}  // namespace netconf
}  // namespace isc