Skip to content

Commit 7c5519c

Browse files
committed
MDEV-22387: Do not violate __attribute__((nonnull))
Passing a null pointer to a nonnull argument is not only undefined behaviour, but it also grants the compiler the permission to optimize away further checks whether the pointer is null. GCC -O2 at least starting with version 8 may do that, potentially causing SIGSEGV.
1 parent 70960bd commit 7c5519c

File tree

3 files changed

+10
-6
lines changed

3 files changed

+10
-6
lines changed

sql/protocol.cc

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* Copyright (c) 2000, 2012, Oracle and/or its affiliates.
2-
Copyright (c) 2008, 2012, Monty Program Ab
2+
Copyright (c) 2008, 2020, MariaDB Corporation.
33
44
This program is free software; you can redistribute it and/or modify
55
it under the terms of the GNU General Public License as published by
@@ -58,7 +58,8 @@ bool Protocol_binary::net_store_data(const uchar *from, size_t length)
5858
packet->realloc(packet_length+9+length))
5959
return 1;
6060
uchar *to= net_store_length((uchar*) packet->ptr()+packet_length, length);
61-
memcpy(to,from,length);
61+
if (length)
62+
memcpy(to,from,length);
6263
packet->length((uint) (to+length-(uchar*) packet->ptr()));
6364
return 0;
6465
}
@@ -715,7 +716,8 @@ void net_send_progress_packet(THD *thd)
715716
uchar *net_store_data(uchar *to, const uchar *from, size_t length)
716717
{
717718
to=net_store_length_fast(to,length);
718-
memcpy(to,from,length);
719+
if (length)
720+
memcpy(to,from,length);
719721
return to+length;
720722
}
721723

sql/sql_string.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -826,7 +826,7 @@ int sortcmp(const String *s,const String *t, CHARSET_INFO *cs)
826826
int stringcmp(const String *s,const String *t)
827827
{
828828
uint32 s_len=s->length(),t_len=t->length(),len=MY_MIN(s_len,t_len);
829-
int cmp= memcmp(s->ptr(), t->ptr(), len);
829+
int cmp= len ? memcmp(s->ptr(), t->ptr(), len) : 0;
830830
return (cmp) ? cmp : (int) (s_len - t_len);
831831
}
832832

strings/ctype-mb.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* Copyright (c) 2000, 2014, Oracle and/or its affiliates.
2-
Copyright (c) 2009, 2014, SkySQL Ab.
2+
Copyright (c) 2009, 2020, MariaDB Corporation.
33
44
This program is free software; you can redistribute it and/or modify
55
it under the terms of the GNU General Public License as published by
@@ -407,7 +407,9 @@ my_copy_fix_mb(CHARSET_INFO *cs,
407407
src, src + src_length,
408408
nchars, status);
409409
DBUG_ASSERT(well_formed_nchars <= nchars);
410-
memmove(dst, src, (well_formed_length= status->m_source_end_pos - src));
410+
well_formed_length= status->m_source_end_pos - src;
411+
if (well_formed_length)
412+
memmove(dst, src, well_formed_length);
411413
if (!status->m_well_formed_error_pos)
412414
return well_formed_length;
413415

0 commit comments

Comments
 (0)