Bug #101715 Unused local variables report
Submitted: 23 Nov 2020 7:48 Modified: 26 Nov 2020 20:03
Reporter: Li Zhong Email Updates:
Status: Verified Impact on me:
None 
Category:MySQL Server: Compiling Severity:S4 (Feature request)
Version: OS:Any
Assigned to: CPU Architecture:Any

[23 Nov 2020 7:48] Li Zhong
Description:
Hi! I've recently used some tool to check MySQL code (master branch, newest version) and find that there are a lot of unused value assignments in the code base. When a developer assigns some value to a variable, the expectation is to use the value sometimes. So an unused assignment could potentially indicate bugs. 

To categorize, the unused assignments we find in MySQL code base include:

1. Return value doesn’t get handled, which may indicate error status from the callee.

Variable Name	|  File Name				|  Line Number	
----------------|---------------------------------------|--------------------
result		|sql/sql_base.cc			|3771
result		|sql/sql_base.cc			|3773
my_ret		|storage/innobase/os/os0enc.cc	        |1214
ret		|storage/innobase/os/os0enc.cc	        |204
ret		|sql/event_queue.cc			|447
result		|sql/auth/sql_user.cc			|2452
result		|sql/auth/sql_user.cc			|2631
err		|storage/innobase/arch/arch0recv.cc	|42

Since those return value could indicate a failure from the caller, I guess some handling of these return value is necessary, like an assert() or some other handling code.

2. Unnecessary initialization that will be covered in following code:
Variable Name	|  File Name				|  Line Number	
----------------|---------------------------------------|--------------------
entry		|storage/innobase/row/row0mysql.cc	|1916
mb		|strings/ctype-mb.cc			|259
mb		|strings/ctype-mb.cc			|989
start_of_record	|sql/sorting_iterator.cc			|617

3. Variables do not get used at all. In this case it may need to be marked with MY_ATTRIBUTE((unused)).
Variable Name	|  File Name				|  Line Number	
--------------- |---------------------------------------|--------------------
derived_tab_no	|sql/sql_resolver.cc			|6836

4. Unnecessary assignments that do not get really used. But it does not seem to affect the correctness of the code.
Variable Name	|  File Name				|  Line Number	
----------------|---------------------------------------|--------------------
to_user		|sql/auth/sql_authorization.cc		|7417 (reported before in https://bugs.mysql.com/bug.php?id=101313)
to_host		|sql/auth/sql_authorization.cc		|7416 (reported before in https://bugs.mysql.com/bug.php?id=101313)
host		|sql/auth/sql_security_ctx.cc		|1144
		
Although they are assigned with “”, their value of “” is not really used anywhere. It may impede the readability of the code.

5. Unused assignments that may potentially be bugs. Since I’m not sure why they get unused, I list them there for you to confirm if you would like to take a bit of time for them.

Variable Name	|  File Name				|  Line Number	
----------------|---------------------------------------|--------------------
log_start	|storage/innobase/mtr/mtr0log.cc	|418
optr		|extra/lz4/lz4hc.c			|552
opSaved	        |extra/lz4/lz4hc.c			|1280
sig_count	|storage/innobase/buf/buf0dblwr.cc	|301
savepoint	|storage/innobase/fts/fts0fts.cc	|5621
data_len	|storage/innobase/clone/clone0snapshot.cc	|963
col_no		|storage/innobase/row/row0upd.cc	|1335
col_no		|storage/innobase/trx/trx0rec.cc	|1999
mysql_data	|storage/innobase/handler/ha_innodb.cc	|8360
res		|sql/binlog.cc				|9072
res		|sql/binlog.cc				|9082
buf		|sql/rpl_slave.cc			|7887
place		|sql/item.cc				|7518
cause		|sql/sql_optimizer.cc			|7827

I will appreciate it so much if you take time to confirm, especially for the 1st and 5th category! 

Also, for the 2-4 categories, I wonder whether I can help to remove them. After all, their existence can either invoke compiler warnings or make the code readability worse.

Thank you for your patient reading!

How to repeat:
It's in the newest version of MySQL-8
[26 Nov 2020 19:09] MySQL Verification Team
Hi,

Thanks for the report. What tool have you bin using?

thanks
Bogdan

p.s. I'm not setting this as a bug but as a feature request to remove unused variables from the code
[26 Nov 2020 20:03] Li Zhong
It's a tool developed by our team. But I think the accuracy is not bad and we manually check through to filter out the false positive.

Since I think the unused assignments of variables could potentially indicate bugs, I wonder if it's worth to take a look at.

Thanks for your labeling, and happy thanksgiving :-)