Disallow variable-length arrays and turn on -Wpedantic in Clang and GCC.

PiperOrigin-RevId: 562832747
Change-Id: I0532624cee31e089a47426305d885687eb0fc5c5
This commit is contained in:
Saran Tunyasuvunakool
2023-09-05 11:04:12 -07:00
committed by Copybara-Service
parent 8ffdee355a
commit 329ed193ac
17 changed files with 121 additions and 99 deletions
+3 -1
View File
@@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
void _mj_crossplatform_void(void) {} // ISO C does not permit empty translation units
#if defined(__APPLE__) && defined(__AVX__)
#include <stdio.h>
@@ -23,7 +25,7 @@ __attribute__((weak, visibility("default"))) void _mj_rosettaError(const char* m
__asm__ __volatile__ ("ud2"); // raises SIGILL but leave this function at the top of the stack
}
__attribute__((constructor(10000), target("no-avx"))) static void _mj_checkRosetta() {
__attribute__((constructor(10000), target("no-avx"))) static void _mj_checkRosetta(void) {
int is_translated = 0;
{
size_t len = sizeof(is_translated);
+2 -2
View File
@@ -32,10 +32,10 @@ MJAPI int mjp_registerPlugin(const mjpPlugin* plugin);
MJAPI int mjp_registerResourceProvider(const mjpResourceProvider* provider);
// return the number of globally registered plugins
MJAPI int mjp_pluginCount();
MJAPI int mjp_pluginCount(void);
// return the number of globally registered resource providers
MJAPI int mjp_resourceProviderCount();
MJAPI int mjp_resourceProviderCount(void);
// look up a plugin by name, optionally also get its registered slot number
MJAPI const mjpPlugin* mjp_getPlugin(const char* name, int* slot);
+6 -3
View File
@@ -14,10 +14,13 @@
#include "engine/engine_support.h"
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <mujoco/mjdata.h>
#include <mujoco/mjmacro.h>
#include <mujoco/mjmodel.h>
#include "engine/engine_array_safety.h"
#include "engine/engine_core_constraint.h"
#include "engine/engine_crossplatform.h"
#include "engine/engine_io.h"
@@ -1056,7 +1059,7 @@ void mj_makeMSparse(const mjModel* m, mjData* d, mjtNum* M,
}
// backward pass over dofs: construct M_row(i) in reverse order
int col = M_rowadr[i]; // current column in row i
int col = M_rowadr[i]; // current column in row i
for (int j = i; j >= 0; j = m->dof_parentid[j]) {
M[col] = d->qM[Madr++];
M_colind[col++] = j;
@@ -1584,7 +1587,7 @@ int mj_version(void) {
// current version of MuJoCo as a null-terminated string
const char* mj_versionString() {
const char* mj_versionString(void) {
static const char versionstring[] = mjVERSIONSTRING;
return versionstring;
}
+4 -1
View File
@@ -15,9 +15,12 @@
#ifndef MUJOCO_SRC_ENGINE_ENGINE_SUPPORT_H_
#define MUJOCO_SRC_ENGINE_ENGINE_SUPPORT_H_
#include <stdint.h>
#include <mujoco/mjdata.h>
#include <mujoco/mjexport.h>
#include <mujoco/mjmodel.h>
#include <mujoco/mjtnum.h>
#ifdef __cplusplus
extern "C" {
@@ -197,7 +200,7 @@ MJAPI void mj_warning(mjData* d, int warning, int info);
MJAPI int mj_version(void);
// current version of MuJoCo as a null-terminated string
MJAPI const char* mj_versionString();
MJAPI const char* mj_versionString(void);
#ifdef __cplusplus
}
#endif
+18 -11
View File
@@ -70,7 +70,7 @@ typedef void (*callback_fn)(const char*);
static mjTHREADLOCAL callback_fn _mjPRIVATE_tls_error_fn = NULL;
static mjTHREADLOCAL callback_fn _mjPRIVATE_tls_warning_fn = NULL;
callback_fn _mjPRIVATE__get_tls_error_fn() {
callback_fn _mjPRIVATE__get_tls_error_fn(void) {
return _mjPRIVATE_tls_error_fn;
}
@@ -78,7 +78,7 @@ void _mjPRIVATE__set_tls_error_fn(callback_fn h) {
_mjPRIVATE_tls_error_fn = h;
}
callback_fn _mjPRIVATE__get_tls_warning_fn() {
callback_fn _mjPRIVATE__get_tls_warning_fn(void) {
return _mjPRIVATE_tls_warning_fn;
}
@@ -113,20 +113,17 @@ void mju_writeLog(const char* type, const char* msg) {
}
}
void mju_error_v(const char* msg, va_list args) {
char errmsg[1000];
// Format msg into errmsg
vsnprintf(errmsg, mjSIZEOFARRAY(errmsg), msg, args);
void mju_error_raw(const char* msg) {
if (_mjPRIVATE_tls_error_fn) {
_mjPRIVATE_tls_error_fn(errmsg);
_mjPRIVATE_tls_error_fn(msg);
} else if (mju_user_error) {
mju_user_error(errmsg);
mju_user_error(msg);
} else {
// write to log and console
mju_writeLog("ERROR", errmsg);
printf("ERROR: %s\n\nPress Enter to exit ...", errmsg);
mju_writeLog("ERROR", msg);
printf("ERROR: %s\n\nPress Enter to exit ...", msg);
// pause, exit
getchar();
@@ -135,6 +132,16 @@ void mju_error_v(const char* msg, va_list args) {
}
void mju_error_v(const char* msg, va_list args) {
// Format msg into errmsg
char errmsg[1024];
vsnprintf(errmsg, mjSIZEOFARRAY(errmsg), msg, args);
mju_error_raw(errmsg);
}
// write message to logfile and console, pause and exit
void mju_error(const char* msg, ...) {
va_list args;
@@ -147,7 +154,7 @@ void mju_error(const char* msg, ...) {
// write message to logfile and console
void mju_warning(const char* msg, ...) {
char wrnmsg[1000];
char wrnmsg[1024];
// Format msg into wrnmsg
va_list args;
+13 -38
View File
@@ -18,6 +18,7 @@
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <mujoco/mjexport.h>
#include <mujoco/mjmacro.h>
@@ -31,8 +32,8 @@ extern "C" {
#define mjPRINTFLIKE(n, m) __attribute__((format(printf, n, m)))
#else
#define mjPRINTFLIKE(n, m)
#endif // __GNUC__
#endif // mjPRINTFLIKE
#endif // __GNUC__
#endif // mjPRINTFLIKE
//------------------------------ user handlers -----------------------------------------------------
@@ -54,6 +55,7 @@ MJAPI void _mjPRIVATE__set_tls_warning_fn(void (*h)(const char*));
//------------------------------ errors and warnings -----------------------------------------------
// errors
MJAPI void mju_error_raw(const char* msg);
MJAPI void mju_error(const char* msg, ...) mjPRINTFLIKE(1, 2);
MJAPI void mju_error_v(const char* msg, va_list args);
MJAPI void mju_error_i(const char* msg, int i);
@@ -69,42 +71,15 @@ MJAPI void mju_writeLog(const char* type, const char* msg);
//------------------------------ internal error macros --------------------------------------------
// need at least c99 or c++11
#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || \
(defined(__cplusplus) && __cplusplus >= 201103L)
// macro to get the first argument
#define _GET_MSG(msg, ...) msg
// helper function for the mjERROR macro
// formats buf as '{prefix}: {msg}' and passes along to mju_error_v
static inline void _mju_error_prefix(char *buf, size_t nbuf, const char* prefix,
const char* msg, ...) mjPRINTFLIKE(4, 5);
static inline void _mju_error_prefix(char *buf, size_t nbuf, const char* prefix,
const char* msg, ...) {
snprintf(buf, nbuf, "%s: %s", prefix, msg);
va_list args;
va_start(args, msg);
mju_error_v(buf, args);
va_end(args);
}
// macro to get first argument
#define _GET_MSG(msg, ...) msg
// internal macro to prepend the calling function name to the error message
// standard support for variadic macros with zero arguments is only now
// supported in C23 and C++20 so we rely on a helper function to get around this
// in a portable way
#define mjERROR(...) { \
char _buf[sizeof(_GET_MSG(__VA_ARGS__)) + sizeof(__func__) + 1]; \
_mju_error_prefix(_buf, sizeof(_buf), __func__, __VA_ARGS__); \
}
#else
#define mjERROR mju_error
#endif // c99 or c++11
// internal macro to prepend the calling function name to the error message
#define mjERROR(...) \
{ \
char _errbuf[1024]; \
size_t _funclen = strlen(__func__); \
strncpy(_errbuf, __func__, sizeof(_errbuf)); \
snprintf(_errbuf + _funclen, sizeof(_errbuf) - _funclen, ": " __VA_ARGS__); \
mju_error_raw(_errbuf); \
}
//------------------------------ malloc and free ---------------------------------------------------