Revision: 76430
http://sourceforge.net/p/brlcad/code/76430
Author: starseeker
Date: 2020-07-22 20:56:14 +0000 (Wed, 22 Jul 2020)
Log Message:
-----------
Enable detection of incorrect/unexpected argv0 values when running GED plugins
Modified Paths:
--------------
brlcad/branches/gedplugins/include/ged/defines.h
brlcad/branches/gedplugins/src/libged/exec_mapping.cpp
brlcad/branches/gedplugins/src/libged/ged_init.cpp
brlcad/branches/gedplugins/src/libged/tests/plugins.c
Modified: brlcad/branches/gedplugins/include/ged/defines.h
===================================================================
--- brlcad/branches/gedplugins/include/ged/defines.h 2020-07-22 20:55:20 UTC
(rev 76429)
+++ brlcad/branches/gedplugins/include/ged/defines.h 2020-07-22 20:56:14 UTC
(rev 76430)
@@ -289,6 +289,8 @@
int cmd_cnt;
};
+/* Report any messages from libged when plugins were initially loaded.
+ * Can be important when diagnosing command errors. */
GED_EXPORT const char * ged_init_msgs();
/* LIBGED maintains this list - callers should regard it as read only. This
@@ -297,6 +299,21 @@
* call to get an updated list and size if commands are altered. */
GED_EXPORT size_t ged_cmd_list(const char * const **cmd_list);
+/* Report whether a string identifies a valid LIBGED command. If func is
+ * non-NULL, check that cmd and func both refer to the same function pointer
+ * (i.e., they are aliases for the same command.)
+ *
+ * If func is NULL, a 0 return indicates an valid GED command and non-zero
+ * indicates a valid command.
+ *
+ * If func is non-null:
+ * 0 indicates both cmd and func strings invoke the same LIBGED function
+ * 1 indicates that either or both of cmd and func were invalid GED commands
+ * 2 indicates that both were valid commands, but they did not match.
+ */
+GED_EXPORT int ged_cmd_valid(const char *cmd, const char *func);
+
+
__END_DECLS
#endif /* GED_DEFINES_H */
Modified: brlcad/branches/gedplugins/src/libged/exec_mapping.cpp
===================================================================
--- brlcad/branches/gedplugins/src/libged/exec_mapping.cpp 2020-07-22
20:55:20 UTC (rev 76429)
+++ brlcad/branches/gedplugins/src/libged/exec_mapping.cpp 2020-07-22
20:56:14 UTC (rev 76430)
@@ -32,10 +32,12 @@
#define GED_CMD(x) \
int GED_CMD_HELPER1(ged_,x)(struct ged *gedp, int argc, const char
*argv[]) \
{ \
- int ret = ged_exec(gedp, argc, argv); \
- if (ret & GED_UNKNOWN) { \
- bu_log("Error: LIBGED command \"ged_" #x "\" called with an
unexpected argv[0] value: %s\n", argv[0]); \
+ const char *fname = #x ; \
+ int ret = ged_cmd_valid(argv[0], fname); \
+ if (ret) { \
+ bu_log("Error(%d): LIBGED command \"ged_" #x "\" called with an
unexpected argv[0] value: %s\n", ret, argv[0]); \
}\
+ ret = ged_exec(gedp, argc, argv); \
return ret; \
} \
Modified: brlcad/branches/gedplugins/src/libged/ged_init.cpp
===================================================================
--- brlcad/branches/gedplugins/src/libged/ged_init.cpp 2020-07-22 20:55:20 UTC
(rev 76429)
+++ brlcad/branches/gedplugins/src/libged/ged_init.cpp 2020-07-22 20:56:14 UTC
(rev 76430)
@@ -48,13 +48,6 @@
static char **cmd_list = NULL;
void *ged_cmds;
-// TODO - when defining commands, should note (when we know) that
-// some commands are aliases for others. Initial application is
-// to validate ged_* function call argv0 strings for correctness
-// to the function name, not just general ged_exec validity, but
-// there may be other uses...
-static std::map<std::string, std::set<std::string>> ged_validity;
-
static std::set<void *> ged_handles;
struct bu_vls *ged_init_msg_str;
@@ -64,6 +57,38 @@
return bu_vls_cstr(ged_init_msg_str);
}
+/* If func is NULL, just see if the string has a ged_cmd_map entry.
+ * If func is defined, see if a) func and cmd have ged_cmd_map entries and
+ * b) if they both do, whether they map to the same function. */
+int
+ged_cmd_valid(const char *cmd, const char *func)
+{
+ if (!cmd) {
+ return 1;
+ }
+ int cmd_invalid = 1;
+ std::string scmd(cmd);
+ std::map<std::string, const struct ged_cmd *>::iterator cmd_it =
ged_cmd_map.find(scmd);
+ if (cmd_it != ged_cmd_map.end()) {
+ cmd_invalid = 0;
+ }
+ if (cmd_invalid) {
+ return cmd_invalid;
+ }
+ ged_func_ptr c1 = cmd_it->second->i->cmd;
+ std::map<std::string, const struct ged_cmd *>::iterator func_it =
ged_cmd_map.find(std::string(func));
+ if (func_it == ged_cmd_map.end()) {
+ // func not in table, nothing to validate against - return invalid
+ return 1;
+ }
+ ged_func_ptr c2 = func_it->second->i->cmd;
+ int mismatched_functions = 2;
+ if (c1 == c2) {
+ mismatched_functions = 0;
+ }
+ return mismatched_functions;
+}
+
size_t
ged_cmd_list(const char * const **cl)
{
Modified: brlcad/branches/gedplugins/src/libged/tests/plugins.c
===================================================================
--- brlcad/branches/gedplugins/src/libged/tests/plugins.c 2020-07-22
20:55:20 UTC (rev 76429)
+++ brlcad/branches/gedplugins/src/libged/tests/plugins.c 2020-07-22
20:56:14 UTC (rev 76430)
@@ -359,15 +359,29 @@
ccmd = app_cmds[app_cmd_cnt];
}
- /* Deliberately call a ged function with wrong argv[0] */
- const char *wav0 = "wrong_name";
- int ret = ged_ls(gbp, 1, &wav0);
- if (ret & GED_UNKNOWN) {
- bu_log("\nged_ls called with command name \"%s\" reported expected
error\n", wav0);
- } else {
- bu_log("\nged_ls called with command name \"%s\" did not report the
expected error\n", wav0);
+ /* Deliberately call a ged function with invalid argv[0] */
+ {
+ const char *wav0 = "wrong_name";
+ int ret = ged_ls(gbp, 1, &wav0);
+ if (ret & GED_UNKNOWN) {
+ bu_log("\nged_ls called with command name \"%s\" reported expected
error\n", wav0);
+ } else {
+ bu_log("\nged_ls called with command name \"%s\" did not report the
expected error\n", wav0);
+ }
}
+ /* Deliberately call a ged function with wrong argv[0] (which is a valid
+ * command, just not one matching the function) */
+ {
+ const char *wav1 = "search";
+ int ret = ged_ls(gbp, 1, &wav1);
+ if (ret & GED_UNKNOWN) {
+ bu_log("\nged_ls called with command name \"%s\" reported expected
error\n", wav1);
+ } else {
+ bu_log("\nged_ls called with command name \"%s\" did not report the
expected error\n", wav1);
+ }
+ }
+
ged_close(gbp);
BU_PUT(gbp, struct ged);
return 0;
This was sent by the SourceForge.net collaborative development platform, the
world's largest Open Source development site.
_______________________________________________
BRL-CAD Source Commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/brlcad-commits