diff options
author | Christian Grothoff <christian@grothoff.org> | 2018-04-05 10:50:15 +0200 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2018-04-05 10:50:45 +0200 |
commit | eea5388958b3c21336972a0c979dc344e3bc66e7 (patch) | |
tree | a3148883f22077f62340ec58ed12c0738a190a64 /src/include | |
parent | e645cf2194cf309c9597b21dc7d4fd2d011777e0 (diff) | |
download | gnunet-eea5388958b3c21336972a0c979dc344e3bc66e7.tar.gz gnunet-eea5388958b3c21336972a0c979dc344e3bc66e7.zip |
introduce GNUNET_NZL macro
Diffstat (limited to 'src/include')
-rw-r--r-- | src/include/gnunet_common.h | 19 |
1 files changed, 16 insertions, 3 deletions
diff --git a/src/include/gnunet_common.h b/src/include/gnunet_common.h index 7d23e6f9b..0fb39575c 100644 --- a/src/include/gnunet_common.h +++ b/src/include/gnunet_common.h | |||
@@ -160,6 +160,19 @@ extern "C" | |||
160 | #endif | 160 | #endif |
161 | 161 | ||
162 | 162 | ||
163 | /** | ||
164 | * Macro used to avoid using 0 for the length of a variable-size | ||
165 | * array (Non-Zero-Length). | ||
166 | * | ||
167 | * Basically, C standard says that "int[n] x;" is undefined if n=0. | ||
168 | * This was supposed to prevent issues with pointer aliasing. | ||
169 | * However, C compilers may conclude that n!=0 as n=0 would be | ||
170 | * undefined, and then optimize under the assumption n!=0, which | ||
171 | * could cause actual issues. Hence, when initializing an array | ||
172 | * on the stack with a variable-length that might be zero, write | ||
173 | * "int[GNUNET_NZL(n)] x;" instead of "int[n] x". | ||
174 | */ | ||
175 | #define GNUNET_NZL(l) GNUNET_MAX(1,l) | ||
163 | 176 | ||
164 | 177 | ||
165 | /** | 178 | /** |
@@ -988,7 +1001,7 @@ GNUNET_ntoh_double (double d); | |||
988 | * arr is important since size is the number of elements and | 1001 | * arr is important since size is the number of elements and |
989 | * not the size in bytes | 1002 | * not the size in bytes |
990 | * @param size the number of elements in the existing vector (number | 1003 | * @param size the number of elements in the existing vector (number |
991 | * of elements to copy over), will be updated with the new | 1004 | * of elements to copy over), will be updated with the new |
992 | * array size | 1005 | * array size |
993 | * @param tsize the target size for the resulting vector, use 0 to | 1006 | * @param tsize the target size for the resulting vector, use 0 to |
994 | * free the vector (then, arr will be NULL afterwards). | 1007 | * free the vector (then, arr will be NULL afterwards). |
@@ -998,13 +1011,13 @@ GNUNET_ntoh_double (double d); | |||
998 | /** | 1011 | /** |
999 | * @ingroup memory | 1012 | * @ingroup memory |
1000 | * Append an element to a list (growing the list by one). | 1013 | * Append an element to a list (growing the list by one). |
1001 | * | 1014 | * |
1002 | * @param arr base-pointer of the vector, may be NULL if size is 0; | 1015 | * @param arr base-pointer of the vector, may be NULL if size is 0; |
1003 | * will be updated to reflect the new address. The TYPE of | 1016 | * will be updated to reflect the new address. The TYPE of |
1004 | * arr is important since size is the number of elements and | 1017 | * arr is important since size is the number of elements and |
1005 | * not the size in bytes | 1018 | * not the size in bytes |
1006 | * @param size the number of elements in the existing vector (number | 1019 | * @param size the number of elements in the existing vector (number |
1007 | * of elements to copy over), will be updated with the new | 1020 | * of elements to copy over), will be updated with the new |
1008 | * array size | 1021 | * array size |
1009 | * @param element the element that will be appended to the array | 1022 | * @param element the element that will be appended to the array |
1010 | */ | 1023 | */ |