Codeigniter: Loader.php _ci_load_library method has a bug

Created on 27 Jun 2017  ·  17Comments  ·  Source: bcit-ci/CodeIgniter

image
if first loaded library set object_name Initialized library.
second loaded library not set object_name, result is return.
so, loaded library fail.

:) sorry, I do not speak English, hope you can understand what I mean!

Bug

Most helpful comment

What we're fixing here is an erroneous property existence check, while what you suggested is forcefully modifying another variable, which alters other logic down the line. Nobody said it's incorrect code, but it does more than we need - a hacky approach towards bug fixing. That's how you end up with a bugfix causing 2 new bugs.

It may turn out to be just fine, but I haven't evaluated or tested it. If it happens to reduce complexity, we may do that as well, but not as a part of this bugfix.

All 17 comments

Please test with the commit linked above.

Also, for future reference - please provide actual code and/or text instead of screenshots.

Isn't it maybe better to always load a library as an alias?

if( $object_name === NULL )
{
$object_name = $library;
}

$this->_ci_load_library($library, $params, $object_name);
return $this;

@narfbg

eg:
$this->load->library('test', null, 'test1');
$this->load->library('test');
because first load library included 'test' class,
so second load library don't included 'test' class, and no set object_name, then return void.

I use version is 2.2.1, may be used method not be the same.

Fixed the bug in the version 2.2.1

if (in_array($filepath, $this->_ci_loaded_files))
{

// Before we deem this to be a duplicate request, let's see
// if a custom object name is being supplied.  If so, we'll
// return a new instance of the object
$CI =& get_instance();
$strLowerClass = strtolower($class);
$classVar = $this->getClassVar($strLowerClass, $object_name);
if ( ! isset($CI->$classVar))
{
    return $this->_ci_init_class($class, '', $params, $object_name);
}
$is_duplicate = TRUE;
log_message('debug', $class." class already loaded. Second attempt ignored.");
return;

}

protected function getClassVar($class, $object_name) {
if (is_null($object_name))
{
$classvar = ( ! isset($this->_ci_varmap[$class])) ? $class : $this->_ci_varmap[$class];
}
else
{
$classvar = $object_name;
}
return $classvar;
}

@GrahamDj No.
@ppanphper CI2 is no longer supported. Stop using it.

@narfbg Production project... :(

That only means you need to upgrade quicker.

@narfbg
Your fix does not work on stock libraries

$this->load->library("email", null, "myemail");
$this->load->library("email");

var_dump($this->myemail); // Object
var_dump($this->email);exit; // NULL

@narfbg second param if set '', isset($property) is true

@GrahamDj Good point, fixed in next commit.
@ppanphper Figured that out while fixing the stock libs ... also fixed.

@narfbg
Just wondering but why is this not a correct fix for the issue?

$prefix = 'CI_';

if( ( class_exists($prefix.$class, FALSE) || class_exists($class, FALSE) ) && empty($object_name) )
{
$object_name = strtolower($class);
}

What we're fixing here is an erroneous property existence check, while what you suggested is forcefully modifying another variable, which alters other logic down the line. Nobody said it's incorrect code, but it does more than we need - a hacky approach towards bug fixing. That's how you end up with a bugfix causing 2 new bugs.

It may turn out to be just fine, but I haven't evaluated or tested it. If it happens to reduce complexity, we may do that as well, but not as a part of this bugfix.

@narfbg
Thanks for the info

Maybe I'm missing something but I don't think this does anything

isset($this->_ci_varmap[$property]) && $property = $this->_ci_varmap[$property];

No where can I find the requested class being added to the array _ci_varmap

But it is being added to the array _ci_classes on line 1278

Ah yes, I now see whats going on :)

It's worth noting that after this fix (i.e. between 3.1.5 and 3.1.6) if you have two libraries that load each other you will always hit a nested loop limit as the libraries will endlessly try and load each other. Just for anyone upgrading from earlier versions of codeigniter, using some "interesting" coding techniques, to the latest.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

monski picture monski  ·  4Comments

boblennes picture boblennes  ·  4Comments

huehnerhose picture huehnerhose  ·  6Comments

al-ramadhan picture al-ramadhan  ·  6Comments

Tjoosten picture Tjoosten  ·  6Comments