Christian Mayer's Weblog

Unused code in call_category_loads()

While hacking around for the Mach-O-project I discovered a unused code block in the source code of Objective-C, version 4.646. Especially in file objc4-646/runtime/objc-loadmethod.mm, function call_category_loads(). This function is called on every App start on every Mac using OS X 10.10 Yosemite.

It’s caused by the variable loadable_categories_used. Take a closer look to call_category_loads.c:

// Original: http://www.opensource.apple.com/source/objc4/objc4-646/runtime/objc-loadmethod.mm

static BOOL call_category_loads(void){
	// ...
	int used = loadable_categories_used;
	// ...
	loadable_categories_used = 0;
	// ...

	new_categories_added = (loadable_categories_used > 0);
	for (i = 0; i < loadable_categories_used; i++) {
		if (used == allocated) {
			allocated = allocated*2 + 16;
			cats = (struct loadable_category *)
			_realloc_internal(cats, allocated * sizeof(struct loadable_category));
		}
		cats[used++] = loadable_categories[i];
	}

	if (loadable_categories) _free_internal(loadable_categories);

	if (used) {
		loadable_categories = cats;
		// ...
	} else {
		// ...
		loadable_categories = nil;
		// ...
	}

	// ...
	return new_categories_added;
}

Between

loadable_categories_used = 0;

and

new_categories_added = (loadable_categories_used > 0);
for (i = 0; i < loadable_categories_used; i++) {

the variable loadable_categories_used is not used. So this function returns always false. The code in the for-loop will never be reached.

The same case occurs with variable loadable_categories. Even if the loop is running the line

cats[used++] = loadable_categories[i];

cause an error because loadable_categories is nil at this point.

To be sure I compiled my own version of this code block. It seems that this is legit.

Thanks to Christian Kremser for reviewing. I reported this bug (19846374) to Apple.
Today we made the Matrix a little bit better.

Update, 2015-03-19: It turns out I was wrong. Apple replied me on 2015-02-26:

That code is intended to handle the case where a category +load method calls dlopen() or NSBundle to load another library that itself has more category +load methods. In that case loadable_categories and loadable_categories_used will become populated inside the calls to (*load_method)(cls, SEL_load).

We are now closing this bug report.

More Resources

Posted on .
Categories: Commented, Programming
Tags: Apple, Mac, OS, X, Yosemite, Objc, ObjectiveC, C, Loop, for, dead, unused, code, bug, error

Imprint | Usage | Categories | RSS Feed
Copyright © 2006 by