Invalid hook_schema() result can trash entire schema

bjaspan - July 27, 2008 - 02:44
Project:Drupal
Version:6.x-dev
Component:database system
Category:bug report
Priority:normal
Assigned:Unassigned
Status:needs review
Issue tags:Quick fix
Description

If any module does not return a value from hook_schema(), the entire schema structure ends up being set to NULL because array_merge(array(), NULL) returns NULL (apparently). The attached patch prevents a single buggy module from taking down the system in this way.

IMHO, it should be applied to 6.x as well.

AttachmentSize
get-schema-null.patch847 bytes

#1

bjaspan - July 27, 2008 - 02:44
Status:active» needs review

#2

moshe weitzman - July 27, 2008 - 13:00

This is true of theme registry as well. I think a little defensiveness like this is a good thing.

#3

lilou - October 26, 2008 - 14:53
Status:needs review» needs work

CNW because drupal_get_schema() move into boostrap.inc.

#4

lilou - October 26, 2008 - 14:57
Status:needs work» needs review
AttachmentSize
issue-287647-4.patch 972 bytes

#5

System Message - November 16, 2008 - 21:55
Status:needs review» needs work

The last submitted patch failed testing.

#6

lilou - November 17, 2008 - 13:31

#7

mikejoconnor - December 15, 2008 - 15:45

If the schema is NULL, I don't think we need to do the require. How about this?

AttachmentSize
null_schema.patch 1.06 KB

#8

Crell - December 15, 2008 - 17:42

We can probably go as far as confirming that $current has a count() >= 1, just to be extra safe.

#9

Damien Tournoud - December 15, 2008 - 22:15

#10

mikejoconnor - December 20, 2008 - 14:49

Here's a new patch, including count()

AttachmentSize
null_schema.patch 1.08 KB

#11

chx - December 20, 2008 - 17:18
Status:needs review» won't fix

I believe this to be a very dangerous practice. Where will this stop? How many lines of code will we add which shields us from bad modules? There are many, many ways for a broken module to mess up your Drupal. This is summed up as "we do not babysit broken code".

#12

robertDouglass - December 29, 2008 - 18:42

Can you cast it to an array?

<?php
$current
= (array) module_invoke($module, 'schema');
?>

That's not even 1 line of babysitting code - it's 7 characters.

#13

robertDouglass - December 29, 2008 - 18:43
Status:won't fix» needs work

#14

Damien Tournoud - December 29, 2008 - 18:47

Agreed with Robert. Casting to an array is the way to go.

Plus I don't see how the patch in #10 solves the issue described in the original post ("passing NULL to array_merge() results in NULL").

#15

chx - January 6, 2009 - 21:24

casting is fine. it's a bit of ugly but yeah ok.

#16

Crell - January 7, 2009 - 07:41

Casting sounds fine to me if that's easier. Probably needs a comment line to clarify it then, too.

I don't see that as babysitting broken code as much as keeping a typo from totally hosing an entire devel site. :-)

#17

mikejoconnor - February 6, 2009 - 15:04
Status:needs work» needs review

New patch, casting to an array.

AttachmentSize
null_schema_cast.patch 932 bytes

#18

rszrama - February 19, 2009 - 14:05

So, I intentionally hacked poll_schema(). It threw a couple of warnings but felt good about itself and said it was installed. I installed profile at the same time as my hacked poll, and profile installed just fine. In that regards, I think the patch works fine.

fwiw, I'm with chx on this one, b/c people really just shouldn't be implementing hook_schema() unless they intend it to return a valid schema array. And if for some reason a schema fails to install, I don't think the module should even be marked as installed.

#19

Crell - February 22, 2009 - 06:41
Status:needs review» reviewed & tested by the community

This is not along a critical path, and while one module dying because its author messed up is one thing, screwing up other modules is simply not acceptable. #17 looks good to me.

#20

Dries - February 22, 2009 - 16:34
Status:reviewed & tested by the community» needs work

Sorry but that comment could be a bit more explanatory. Setting back to 'code needs work'.

#21

cafuego - October 28, 2009 - 23:46
Status:needs work» needs review

Argh, this bit me when I stuck a placeholder hook_schema() in a module. Such an easy fix.

Attached original patch with expanded comment. Also see #617546: Invalid hook_schema() result can trash entire schema for the D6 patch.

AttachmentSize
287647.patch 1.05 KB

#22

Crell - October 29, 2009 - 14:59
Status:needs review» reviewed & tested by the community

Safety from bad hooks++

#23

bjaspan - October 30, 2009 - 00:25

I can't believe this little issue has required 6 patches (all basically identical) and 23 comments. Thank you, crell, for the RTBC.

<chant>commit, commit, commit</chant>

#24

webchick - November 1, 2009 - 23:10
Version:7.x-dev» 6.x-dev
Status:reviewed & tested by the community» patch (to be ported)

Comment looks good.

In general, I am against babysitting broken code as well, but Crell in #19 makes a good point that this doesn't just screw up your module (as when you forget to return $items from hook_menu()), this screws up everything.

Committed to HEAD.

Looks like this could be backported to D6 as well?

#25

cafuego - November 4, 2009 - 01:57
Status:patch (to be ported)» needs review

I can't express in words how much I concur that it should be backported to D6. Patch attached.

AttachmentSize
287647.patch 1020 bytes
 
 

Drupal is a registered trademark of Dries Buytaert.