Skip to content

Commit

Permalink
mx_mysql: Remove broken errno range check
Browse files Browse the repository at this point in the history
The assertion

    mx_assert_return_minus_errno((unsigned int)(int)error == error, ERANGE);

doesn't work as intended, the conditional expression is optimized away
to true by gcc with -O1 and up [1]. We could do with

    assert(sizeof(long) > sizeof(int)
    assert(-(long)error >= INT_MIN);

or with

    int ret;
    assert(!__builtin_sub_overflow(0, error, &ret))

to detect values outside of the range [0..-INT_MIN] which could not be
negated into the range [INT_MIN..0].

However, the check seems not to be neccessary, as the mysql error number
is nowhere converted to a negative error number, all callers just use it
to compare it with constants. The other assert checks of the helper
functions are just redundant. Remove the helper functions
mx__mysql_errno() and mx__mysql_stmt_errno() and and use the native
mysql calls instead.

[1]: https://godbolt.org/z/1ajK384Ge
  • Loading branch information
donald committed Dec 29, 2023
1 parent 6c28e16 commit ff46678
Showing 1 changed file with 10 additions and 38 deletions.
48 changes: 10 additions & 38 deletions mx_mysql.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,6 @@ char *mx_mysql_error(void) {
return mx_mysql_last_error == NULL ? "no error information" : mx_mysql_last_error;
}

static int mx__mysql_errno(struct mx_mysql *mysql)
{
unsigned int error;

mx_assert_return_minus_errno(mysql, EINVAL);
mx_assert_return_minus_errno(mysql->mysql, EBADF);

error = mysql_errno(mysql->mysql);
mx_assert_return_minus_errno((unsigned int)(int)error == error, ERANGE);

/* no mysql errors possible */
return (int)error;
}

static const char *mx__mysql_error(struct mx_mysql *mysql)
{
mx_assert_return_NULL(mysql, EINVAL);
Expand All @@ -68,20 +54,6 @@ static const char *mx__mysql_error(struct mx_mysql *mysql)
return mysql_error(mysql->mysql);
}

static int mx__mysql_stmt_errno(struct mx_mysql_stmt *stmt)
{
unsigned int error;

mx_assert_return_minus_errno(stmt, EINVAL);
mx_assert_return_minus_errno(stmt->stmt, EBADF);

error = mysql_stmt_errno(stmt->stmt);
mx_assert_return_minus_errno((unsigned int)(int)error == error, ERANGE);

/* no mysql errors possible */
return (int)error;
}

static int mx__mysql_init(struct mx_mysql *mysql)
{
mx_assert_return_minus_errno(mysql, EINVAL);
Expand Down Expand Up @@ -121,7 +93,7 @@ static int mx__mysql_real_connect(struct mx_mysql *mysql, const char *host, cons

mx_mysql_save_error(mysql_error(mysql->mysql));

switch (mx__mysql_errno(mysql)) {
switch (mysql_errno(mysql->mysql)) {
case CR_ALREADY_CONNECTED:
return -EALREADY;

Expand Down Expand Up @@ -154,7 +126,7 @@ static int mx__mysql_ping(struct mx_mysql *mysql)

mx_mysql_save_error(mysql_error(mysql->mysql));

switch (mx__mysql_errno(mysql)) {
switch (mysql_errno(mysql->mysql)) {
case CR_COMMANDS_OUT_OF_SYNC:
return -EPROTO;

Expand Down Expand Up @@ -210,7 +182,7 @@ static int mx__mysql_stmt_prepare(struct mx_mysql_stmt *stmt, char *statement)
}
mx_mysql_save_error(mysql_stmt_error(stmt->stmt));

switch (mx__mysql_stmt_errno(stmt)) {
switch (mysql_stmt_errno(stmt->stmt)) {
case CR_OUT_OF_MEMORY:
return -ENOMEM;

Expand Down Expand Up @@ -244,7 +216,7 @@ static int mx__mysql_stmt_bind_param(struct mx_mysql_stmt *stmt)

mx_mysql_save_error(mysql_stmt_error(stmt->stmt));

switch (mx__mysql_stmt_errno(stmt)) {
switch (mysql_stmt_errno(stmt->stmt)) {
case CR_OUT_OF_MEMORY:
return -ENOMEM;

Expand All @@ -270,7 +242,7 @@ static int mx__mysql_stmt_bind_result(struct mx_mysql_stmt *stmt)

mx_mysql_save_error(mysql_stmt_error(stmt->stmt));

switch (mx__mysql_stmt_errno(stmt)) {
switch (mysql_stmt_errno(stmt->stmt)) {
case CR_OUT_OF_MEMORY:
return -ENOMEM;

Expand All @@ -296,7 +268,7 @@ static int mx__mysql_stmt_execute(struct mx_mysql_stmt *stmt)

mx_mysql_save_error(mysql_stmt_error(stmt->stmt));

switch (mx__mysql_stmt_errno(stmt)) {
switch (mysql_stmt_errno(stmt->stmt)) {
case CR_COMMANDS_OUT_OF_SYNC:
return -EPROTO;

Expand Down Expand Up @@ -326,7 +298,7 @@ static int mx__mysql_stmt_store_result(struct mx_mysql_stmt *stmt)

mx_mysql_save_error(mysql_stmt_error(stmt->stmt));

switch (mx__mysql_stmt_errno(stmt)) {
switch (mysql_stmt_errno(stmt->stmt)) {
case CR_COMMANDS_OUT_OF_SYNC:
return -EPROTO;

Expand Down Expand Up @@ -372,7 +344,7 @@ static int mx__mysql_stmt_fetch(struct mx_mysql_stmt *stmt)
if (res == 1) {
mx_mysql_save_error(mysql_stmt_error(stmt->stmt));

switch (mx__mysql_stmt_errno(stmt)) {
switch (mysql_stmt_errno(stmt->stmt)) {
case CR_OUT_OF_MEMORY:
return -ENOMEM;

Expand Down Expand Up @@ -412,7 +384,7 @@ static int mx__mysql_stmt_fetch_column(struct mx_mysql_stmt *stmt, unsigned int
return 0;
mx_mysql_save_error(mysql_stmt_error(stmt->stmt));

switch (mx__mysql_stmt_errno(stmt)) {
switch (mysql_stmt_errno(stmt->stmt)) {
case CR_INVALID_PARAMETER_NO:
return -EPROTO;

Expand Down Expand Up @@ -493,7 +465,7 @@ static int mx__mysql_stmt_close(struct mx_mysql_stmt *stmt)
}
mx_mysql_save_error(mysql_stmt_error(stmt->stmt));

switch (mx__mysql_stmt_errno(stmt)) {
switch (mysql_stmt_errno(stmt->stmt)) {
case CR_SERVER_GONE_ERROR:
return -ECONNABORTED;

Expand Down

0 comments on commit ff46678

Please sign in to comment.