[ZBX-9677] C preprocessor directives should not be used in macro arguments Created: 2015 Jul 02  Updated: 2017 May 30  Resolved: 2015 Nov 06

Status: Closed
Project: ZABBIX BUGS AND ISSUES
Component/s: Agent (G), Proxy (P), Server (S)
Affects Version/s: 2.5.0
Fix Version/s: 2.0.16rc1, 2.2.11rc1, 2.4.7rc1, 3.0.0alpha2

Type: Incident report Priority: Trivial
Reporter: Andris Mednis Assignee: Unassigned
Resolution: Fixed Votes: 0
Labels: compilation
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

In file src/zabbix_server/escalator/escalator.c there is a function execute_commands() with code:

        result = DBselect(
                        "select distinct h.hostid,h.host,o.type,o.scriptid,o.execute_on,o.port"
                                ",o.authtype,o.username,o.password,o.publickey,o.privatekey,o.command"
#ifdef HAVE_OPENIPMI
                                ",h.ipmi_authtype,h.ipmi_privilege,h.ipmi_username,h.ipmi_password"
#endif
                        " from opcommand o,opcommand_grp og,hosts_groups hg,hosts h"
                        " where o.operationid=og.operationid"
                                " and og.groupid=hg.groupid"
                                " and hg.hostid=h.hostid"
                                " and o.operationid=" ZBX_FS_UI64
                                " and h.status=%d"
                        " union "
                        "select distinct h.hostid,h.host,o.type,o.scriptid,o.execute_on,o.port"
                                ",o.authtype,o.username,o.password,o.publickey,o.privatekey,o.command"
#ifdef HAVE_OPENIPMI
                                ",h.ipmi_authtype,h.ipmi_privilege,h.ipmi_username,h.ipmi_password"
#endif
                        " from opcommand o,opcommand_hst oh,hosts h"
                        " where o.operationid=oh.operationid"
                                " and oh.hostid=h.hostid"
                                " and o.operationid=" ZBX_FS_UI64
                                " and h.status=%d"
                        " union "
                        "select distinct 0,null,o.type,o.scriptid,o.execute_on,o.port"
                                ",o.authtype,o.username,o.password,o.publickey,o.privatekey,o.command"
#ifdef HAVE_OPENIPMI
                                ",0,2,null,null"
#endif
                        " from opcommand o,opcommand_hst oh"
                        " where o.operationid=oh.operationid"
                                " and o.operationid=" ZBX_FS_UI64
                                " and oh.hostid is null",
                        operationid, HOST_STATUS_MONITORED,
                        operationid, HOST_STATUS_MONITORED,
                        operationid);

DBselect() is defined as macro.
Compiler (GCC 5.1.1) issues warnings:

escalator.c: In function ‘execute_commands’:
escalator.c:469:1: warning: embedding a directive within macro arguments is not portable
 #ifdef HAVE_OPENIPMI
escalator.c:471:1: warning: embedding a directive within macro arguments is not portable
 #endif
escalator.c:481:1: warning: embedding a directive within macro arguments is not portable
 #ifdef HAVE_OPENIPMI
escalator.c:483:1: warning: embedding a directive within macro arguments is not portable
 #endif
escalator.c:492:1: warning: embedding a directive within macro arguments is not portable
 #ifdef HAVE_OPENIPMI
escalator.c:494:1: warning: embedding a directive within macro arguments is not portable
 #endif


 Comments   
Comment by Aleksandrs Saveljevs [ 2015 Jul 02 ]

According to http://stackoverflow.com/questions/28343501/is-it-legal-to-use-the-line-directive-in-a-macro-argument the consequences are undefined:

From the C standard, 6.10.3 Macro replacement:

11 [...] If there are sequences of preprocessing tokens within the list of arguments that would otherwise act as preprocessing directives, the behavior is undefined.

Comment by Aleksandrs Saveljevs [ 2015 Jul 02 ]

(1) Let's also document it in the coding guidelines at https://www.zabbix.org/wiki/C_coding_guidelines .

asaveljevs Documented at https://www.zabbix.org/wiki/C_coding_guidelines#Macros . RESOLVED.

sasha Great! CLOSED

Comment by Igors Homjakovs (Inactive) [ 2015 Jul 07 ]

Fixed in svn://svn.zabbix.com/branches/dev/ZBX-9677

Comment by Andris Zeila [ 2015 Jul 29 ]

(2) The resulting sql looks correct. Just a small comment - we should use zbx_strcpy_alloc() rather than zbx_snprintf_alloc() when simply concatenating strings.

igorsh RESOLVED in r54605.

wiper CLOSED

Comment by Andris Zeila [ 2015 Jul 30 ]

Successfully tested

Comment by Alexander Vladishev [ 2015 Aug 30 ]

Fixed in:

  • pre-2.0.16 r55262
  • pre-2.2.11 r55263
  • pre-2.4.7 r55264
  • pre-3.0.0alpha2 (trunk) r55265
Comment by richlv [ 2015 Sep 04 ]

(3) typo in the changelog entry : ecalator.s

<richlv> should be fixed in r55388.

asaveljevs "escalator.s" is an Assembly file, not a C file. REOPENED

sasha RESOLVED in r55464,r55465

asaveljevs CLOSED

Comment by richlv [ 2015 Sep 04 ]

subissues still open : 1, 3

Generated at Fri Apr 19 19:02:15 EEST 2024 using Jira 9.12.4#9120004-sha1:625303b708afdb767e17cb2838290c41888e9ff0.