Skip to contentSkip to conversation

ASoC: cs35l32: avoid uninitialized variable access

PATCH In review Arnd Bergmann 3 participants 7 messages Jan 1, 2016 – Jan 4, 2016

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ASoC: cs35l32: avoid uninitialized variable access

gcc warns about the possibilty of accessing a property read from devicetree in cs35l32_i2c_probe() when it has not been initialized because CONFIG_OF is disabled:

sound/soc/codecs/cs35l32.c: In function 'cs35l32_i2c_probe':
sound/soc/codecs/cs35l32.c:278:2: warning: 'val' may be used uninitialized in this function [-Wmaybe-uninitialized]

The code is actually correct because it checks the dev->of_node variable first and we know this is NULL here, but by adding a check for IS_ENABLED(CONFIG_OF), we can let the compiler know as well, and also generate smaller object code.

sound/soc/codecs/cs35l32.c +1 -1
index 44c30fe3e315..52ffaa8eb02b 100644
@@ -372,7 +372,7 @@ static int cs35l32_i2c_probe(struct i2c_client *i2c_client,
 			dev_err(&i2c_client->dev, "could not allocate pdata\n");
 			return -ENOMEM;
 		}
-		if (i2c_client->dev.of_node) {
+		if (IS_ENABLED(CONFIG_OF) && i2c_client->dev.of_node) {
 			ret = cs35l32_handle_of_data(i2c_client,
 						     &cs35l32->pdata);
 			if (ret != 0)
maintainer
#1 lore raw

sound/soc/codecs/cs35l32.c static int cs35l32_i2c_probe(struct i2c_client *i2c_client, · line 375

Quoted 2 lines · Arnd Bergmann
sound/soc/codecs/cs35l32.c +1 -1
-		if (i2c_client->dev.of_node) {
+		if (IS_ENABLED(CONFIG_OF) && i2c_client->dev.of_node) {

This would be a lot nicer if there was an __always_null annotation we
could put on of_node for !OF configurations, that'd Just Work and this
can't be the only case where we have this idiom.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 473 bytes
Desc: not available
URL: <http://lists.infradead.org/pipermail/linux-arm-kernel/attachments/20160102/54dc35cc/attachment-0001.sig>

#2 lore raw
Quoted 9 lines · On Saturday 02 January 2016 14:17:46 Mark Brown wrote:

sound/soc/codecs/cs35l32.c static int cs35l32_i2c_probe(struct i2c_client *i2c_client, · line 375

Quoted 2 lines · Arnd Bergmann
sound/soc/codecs/cs35l32.c +1 -1
-             if (i2c_client->dev.of_node) {
+             if (IS_ENABLED(CONFIG_OF) && i2c_client->dev.of_node) {

This would be a lot nicer if there was an __always_null annotation we could put on of_node for !OF configurations, that'd Just Work and this can't be the only case where we have this idiom.

How about an inline helper like

static inline struct device_node *dev_of_node(struct device *dev)

{
	if (IS_ENABLED(CONFIG_OF))
		return dev->of_node;	
}

Arnd

#3 lore raw
Quoted 11 lines · Arnd Bergmann
Quoted 9 lines · On Saturday 02 January 2016 14:17:46 Mark Brown wrote:

sound/soc/codecs/cs35l32.c static int cs35l32_i2c_probe(struct i2c_client *i2c_client, · line 375

Quoted 2 lines · Arnd Bergmann
sound/soc/codecs/cs35l32.c +1 -1
-             if (i2c_client->dev.of_node) {
+             if (IS_ENABLED(CONFIG_OF) && i2c_client->dev.of_node) {

This would be a lot nicer if there was an __always_null annotation we could put on of_node for !OF configurations, that'd Just Work and this can't be the only case where we have this idiom.

Quoted 6 lines

How about an inline helper like

static inline struct device_node *dev_of_node(struct device *dev)

{
	if (IS_ENABLED(CONFIG_OF))
		return dev->of_node;	

ITYM:

	return IS_ENABLED(CONFIG_OF) ? dev->of_node : NULL;

or

	if (IS_ENABLED(CONFIG_OF))
		return dev->of_node;
	else
		return NULL;
Quoted 8 lines

}

Arnd

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

Signature · 3 lines
RMK's Patch system: http://www.arm.linux.org.uk/developer/patches/
FTTC broadband for 0.8mile line: currently at 9.6Mbps down 400kbps up
according to speedtest.net.
maintainer
#6 lore raw
In reply to Arnd Bergmann
Quoted 6 lines · Arnd Bergmann
Quoted 3 lines · On Saturday 02 January 2016 14:17:46 Mark Brown wrote:

This would be a lot nicer if there was an __always_null annotation we could put on of_node for !OF configurations, that'd Just Work and this can't be the only case where we have this idiom.

Quoted 7 lines

How about an inline helper like

static inline struct device_node *dev_of_node(struct device *dev)

{
	if (IS_ENABLED(CONFIG_OF))
		return dev->of_node;	
}

Yeah, that'd work as well (with the correction Russell mentioned) - it's
a bit more typing but we already do similar things for the ID tables and
it looks nicer than the IS_ENABLED() in code does.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 473 bytes
Desc: not available
URL: <http://lists.infradead.org/pipermail/linux-arm-kernel/attachments/20160104/ce59d166/attachment.sig>

#4 lore raw
In reply to Russell King - ARM Linux
Quoted 21 lines · On Monday 04 January 2016 15:20:58 Russell King - ARM Linux wrote:
Quoted 11 lines · Arnd Bergmann
Quoted 9 lines · On Saturday 02 January 2016 14:17:46 Mark Brown wrote:

sound/soc/codecs/cs35l32.c static int cs35l32_i2c_probe(struct i2c_client *i2c_client, · line 375

Quoted 2 lines · Arnd Bergmann
sound/soc/codecs/cs35l32.c +1 -1
-             if (i2c_client->dev.of_node) {
+             if (IS_ENABLED(CONFIG_OF) && i2c_client->dev.of_node) {

This would be a lot nicer if there was an __always_null annotation we could put on of_node for !OF configurations, that'd Just Work and this can't be the only case where we have this idiom.

Quoted 6 lines

How about an inline helper like

static inline struct device_node *dev_of_node(struct device *dev)

{
      if (IS_ENABLED(CONFIG_OF))
              return dev->of_node;    

ITYM:

Quoted 9 lines
        return IS_ENABLED(CONFIG_OF) ? dev->of_node : NULL;

or

        if (IS_ENABLED(CONFIG_OF))
                return dev->of_node;
        else
                return NULL;

Right, yes.

That reminds of a different problem that has been bugging me for a
while: We frequently have a pattern like

#ifdef CONFIG_FOO
static int function(void)

{
	...
}

#endif

struct operations = {
	...
#ifdef CONFIG_FOO
	.function = function;
#endif
	...
};

Except that people constantly get it wrong, e.g. by using the wrong ifdef, forgetting one of the two ifdefs, or by leaving unused static functions that only get called indirectly from the other one that is built conditionally.

We could add a macro like

#define COND_PTR(config, ptr) (IS_ENABLED(config) ? (ptr) : NULL)

and then let the compiler figure out that "function" is unused even without an explicit __maybe_unused annotation. The function above can be simplied to

static inline struct device_node *dev_of_node(struct device *dev)

{
	return COND_PTR(CONFIG_OF, dev->of_node);
}

with that, which is another benefit.

Arnd

#5 lore raw
Quoted 18 lines · Arnd Bergmann

That reminds of a different problem that has been bugging me for a
while: We frequently have a pattern like

#ifdef CONFIG_FOO
static int function(void)

{
	...
}

#endif

struct operations = {
	...
#ifdef CONFIG_FOO
	.function = function;
#endif
	...
};
Quoted 4 lines

Except that people constantly get it wrong, e.g. by using the wrong ifdef, forgetting one of the two ifdefs, or by leaving unused static functions that only get called indirectly from the other one that is built conditionally.

We already have a solution to that. __maybe_unused against the function, and use the correct #ifdef in the structure initialiser. We just need reviewers to be better at picking that up.

Quoted 8 lines

We could add a macro like

#define COND_PTR(config, ptr) (IS_ENABLED(config) ? (ptr) : NULL)

and then let the compiler figure out that "function" is unused even without an explicit __maybe_unused annotation. The function above can be simplied to

Quoted 6 lines

static inline struct device_node *dev_of_node(struct device *dev)

{
	return COND_PTR(CONFIG_OF, dev->of_node);
}

with that, which is another benefit.

You're just inventing another way for people to get it wrong though. Instead of having mismatched #ifdefs, we can now have a mismatched #ifdef around the function and the COND_PTR config - and people will add #ifdef's because they won't realise they don't need them.

You're reliant on reviewers to spotting the pattern, and suggesting using COND_PTR() without #ifdefs around the function. It's the same problem with spotting the existing pattern and suggesting dropping the #ifdef around the function and annotating the function with __maybe_unused.

So, I don't see the benefit.

Signature · 3 lines
RMK's Patch system: http://www.arm.linux.org.uk/developer/patches/
FTTC broadband for 0.8mile line: currently at 9.6Mbps down 400kbps up
according to speedtest.net.