tadman
tadman

Reputation: 211590

Purpose behind comparing string pointer to NULL as well as NULL character

While trying to compile xtrabackup from source I've found a peculiar line of code in sql/sql_acl.cc that GCC is refusing to compile without a more permissive setting. This line is the problem:

if (combo->plugin.str == NULL || combo->plugin.str == '\0')

Which immediately raises the following error:

error: ISO C++ forbids comparison between pointer and integer

This seems entirely reasonable given the code in question. The plugin value is of this type:

struct st_mysql_lex_string
{
  char *str;
  size_t length;
};

Where that's a simple MySQL internal structure that represents a string pointer + length pair, so in this case str is merely char*, nothing special.

I know that cross-platform development and dealing with baroque compiler environments can require a certain level of paranoia, but what, if any, justification is there for this double NULL check? I can't think of how the second clause would ever be true if the first wasn't, but I might be missing some unusual edge case.

Upvotes: 3

Views: 333

Answers (1)

Lesh
Lesh

Reputation: 151

My assumption is that the intention was to support the string where the null termination is optional (therefore there is a length field). In this case, the invalid string that contains '\0' will have the valid size 1. That is why this sanity condition does not use the length. Anyway, there is a bug that makes the second condition always "true" and therefore pointless, the code probably shall look like:

if (combo->plugin.str == NULL || combo->plugin.str [0] == '\0')

Upvotes: 3

Related Questions