The static analysis tool we use puts C code similar to the following, like a critical buffer overflow.
#define size 64 char buf [size + 1] = ""; memset (buf, 0, size + 1);
Tool error message: Buffer overflow (array index out of bounds): The size of the buf array is 1. The buf array can use index 0..64.
Is it legal? Does assigning an array of characters to an empty string really result in its length being reduced to one byte, as if it were defined as char buf [] = "";?
char buf [] = "";
"" buf [size + 1] reset buf, , , memset ( - ).
, char buf [size + 1] , , , buf 65, memset (buf, 0, 65) .
.
[: ]
, :
#define size 64 char buf[size+1]; strcpy(buf, ""); memset(buf, 0, size+1);
, ; , .
This is probably a cleaner way to do this. Of course, this takes less lines of code.
#define size 64 char buf[size + 1] = {0};
This is legal - the buffer is large enough. The tool warns you that size_t may be larger than int, and tries to use it as an indexer, which can lead to unpredictable results.
Source: https://habr.com/ru/post/1703298/More articles:Problem creating email with attachment in javascript - javascriptЯвляется ли этичным следовать политике компании, если вы считаете это неправильным? - internet-explorerSSH, the command entered, but it was not executed, cannot enter anything - bashHow to exclude a specific CSS file from an ASP.NET theme? - asp.netStrange problem - javascript cannot be assigned for reference - asp.nethttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1703299/where-is-setoanocache-defined&usg=ALkJrhhmC8RMDzSJxJaiy3TRmTSYDU-EnATemporary enable ON DELETE CASCADE - sqlCall Directory.Exists ("\\ SERVER \ SHARE \") in Setup Project - installerSubversive (Eclipse SVN plugin) reports that external blocks are closed - eclipseWith MySQL, how can I insert into a table, provided that this value does not exist in another table? - sqlAll Articles