view liboctave/util/pathsearch.cc @ 33240:4e5bc9c4f657

use std::stoi or std::stoul instead of atoi (bug #65342) The std::stoi and std::stoul functions allow us to distinguish between invalid values and out of range values instead of just returning 0 for any conversion failure as atoi does. In the following changes, we now attempt to provide better diagnostics where possible. * pathsearch.cc (directory_path::init): Use std::stoul instead of atoi. Issue warnings for out of range or invalid values. * __init_fltk__.cc (fltk_uimenu::add_entry): Use std::stoi instead of atoi. * event-manager.cc (F__event_manager_file_dialog__): Use std::stoi instead of atoi. * bp-table.cc (bp_table::parse_dbfunction_params): Use std::stoi instead of atoi. * data-conv.cc (oct_data_conv::string_to_data_type): Use std::stoi instead of atoi. * debug.cc (parse_start_end, parse_integer_argument): New static functions. (Fdbtype): Use parse_start_end to improve parsing of integer arguments. (Fdblist, Fdbstack, do_dbupdown): Use parse_integer_argument to improve handling of integer argument values.
author John W. Eaton <jwe@octave.org>
date Sat, 23 Mar 2024 12:13:17 -0400
parents 2e484f9f1f18
children
line wrap: on
line source

////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 1996-2024 The Octave Project Developers
//
// See the file COPYRIGHT.md in the top-level directory of this
// distribution or <https://octave.org/copyright/>.
//
// This file is part of Octave.
//
// Octave 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 3 of the License, or
// (at your option) any later version.
//
// Octave 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 Octave; see the file COPYING.  If not, see
// <https://www.gnu.org/licenses/>.
//
////////////////////////////////////////////////////////////////////////

#if defined (HAVE_CONFIG_H)
#  include "config.h"
#endif

#include <cstdlib>

#include <string>

#include "kpse.h"
#include "lo-error.h"
#include "lo-utils.h"
#include "oct-env.h"
#include "pathsearch.h"

OCTAVE_BEGIN_NAMESPACE(octave)

directory_path::directory_path (const std::string& s)
  : m_orig_path (s), m_initialized (false), m_expanded_path (),
    m_path_elements ()
{
  if (! m_orig_path.empty ())
    init ();
}

std::list<std::string>
directory_path::elements ()
{
  return m_initialized ? m_path_elements : std::list<std::string> ();
}

std::list<std::string>
directory_path::all_directories ()
{
  std::list<std::string> retval;

  if (m_initialized)
    {
      for (const auto& elt : m_path_elements)
        {
          std::string elt_dir = kpse_element_dir (elt);

          if (! elt_dir.empty ())
            retval.push_back (elt_dir);
        }
    }

  return retval;
}

std::string
directory_path::find_first (const std::string& nm)
{
  return m_initialized ? kpse_path_search (m_expanded_path, nm) : "";
}

std::list<std::string>
directory_path::find_all (const std::string& nm)
{
  return (m_initialized
          ? kpse_all_path_search (m_expanded_path, nm)
          : std::list<std::string> ());
}

std::string
directory_path::find_first_of (const std::list<std::string>& names)
{
  return (m_initialized
          ? kpse_path_find_first_of (m_expanded_path, names) : "");
}

std::list<std::string>
directory_path::find_all_first_of (const std::list<std::string>& names)
{
  return (m_initialized
          ? kpse_all_path_find_first_of (m_expanded_path, names)
          : std::list<std::string> ());
}

void
directory_path::init ()
{
  static bool octave_kpse_initialized = false;

  if (! octave_kpse_initialized)
    {
      std::string env_val = sys::env::getenv ("KPATHSEA_DEBUG");

      if (! env_val.empty ())
        {
          unsigned int env_debug_flags = 0;

          try
            {
              unsigned long val = std::stoul (env_val);

              if (val > std::numeric_limits<unsigned int>::max ())
                (*current_liboctave_warning_with_id_handler)
                  ("Octave:kpathsea-debug-value-ignored", "directory_path::init: ignoring out of range KPATHSEA_DEBUG value '%s'", env_val.c_str ());
              else
                env_debug_flags = val;
            }
          catch (const std::invalid_argument&)
              {
                (*current_liboctave_warning_with_id_handler)
                  ("Octave:kpathsea-debug-value-ignored", "directory_path::init: ignoring invalid KPATHSEA_DEBUG value '%s'", env_val.c_str ());
              }
            catch (const std::out_of_range&)
              {
                (*current_liboctave_warning_with_id_handler)
                  ("Octave:kpathsea-debug-value-ignored", "directory_path::init: ignoring out of range KPATHSEA_DEBUG value '%s'", env_val.c_str ());
              }

          kpse_debug |= env_debug_flags;
        }

      octave_kpse_initialized = true;
    }

  m_expanded_path = kpse_path_expand (m_orig_path);

  for (kpse_path_iterator pi (m_expanded_path); pi != std::string::npos; pi++)
    m_path_elements.push_back (*pi);

  m_initialized = true;
}

char
directory_path::path_sep_char ()
{
  return SEPCHAR;
}

std::string
directory_path::path_sep_str ()
{
  return SEPCHAR_STR;
}

OCTAVE_END_NAMESPACE(octave)