Kaynağa Gözat

* Use length() macro for bsearch() calling and also fix help table lookup off-by-1 error

Bryan Drewery 16 yıl önce
ebeveyn
işleme
f45337a580
6 değiştirilmiş dosya ile 6 ekleme ve 5 silme
  1. 1 1
      src/botcmd.c
  2. 1 1
      src/cmds.c
  3. 1 0
      src/common.h
  4. 1 1
      src/mod/share.mod/share.c
  5. 1 1
      src/mod/update.mod/update.c
  6. 1 1
      src/set.c

+ 1 - 1
src/botcmd.c

@@ -1251,7 +1251,7 @@ const botcmd_t *search_botcmd_t(const botcmd_t *table, const char* keyString, si
 }
 
 void parse_botcmd(int idx, const char* code, const char* msg) {
-  const botcmd_t *cmd = search_botcmd_t((const botcmd_t*)&C_bot, code, (sizeof(C_bot)/sizeof(botcmd_t)) - 1);
+  const botcmd_t *cmd = search_botcmd_t((const botcmd_t*)&C_bot, code, lengthof(C_bot) - 1);
 
   if (cmd) {
     /* Found a match */

+ 1 - 1
src/cmds.c

@@ -630,7 +630,7 @@ findcmd(const char *lookup, bool care_about_type)
 {
   help_t key;
   key.cmd = (char*)lookup;
-  help_t *h_entry = (help_t *) bsearch(&key, &help, sizeof(help)/sizeof(help_t), sizeof(help_t), comp_help_t);
+  help_t *h_entry = (help_t *) bsearch(&key, &help, lengthof(help) - 1, sizeof(help_t), comp_help_t);
   if (h_entry && ((care_about_type && have_cmd(h_entry->cmd, h_entry->type)) || (!care_about_type)))
       return h_entry;
 

+ 1 - 0
src/common.h

@@ -99,5 +99,6 @@
 #define unlikely(x)  (x)
 #endif
 
+#define lengthof(x) (sizeof(x)/sizeof(x[0]))
 
 #endif				/* _COMMON_H */

+ 1 - 1
src/mod/share.mod/share.c

@@ -1154,7 +1154,7 @@ void
 sharein(int idx, char *msg)
 {
   char *code = newsplit(&msg);
-  const botcmd_t *cmd = search_botcmd_t((const botcmd_t*)&C_share, code, (sizeof(C_share)/sizeof(botcmd_t)) - 1);
+  const botcmd_t *cmd = search_botcmd_t((const botcmd_t*)&C_share, code, lengthof(C_share) - 1);
   if (cmd) {
     /* Found a match */
     (cmd->func) (idx, msg);

+ 1 - 1
src/mod/update.mod/update.c

@@ -230,7 +230,7 @@ static cmd_t update_bot[] = {
 void updatein(int idx, char *msg)
 {
   char *code = newsplit(&msg);
-  const botcmd_t *cmd = search_botcmd_t((const botcmd_t*)&C_update, code, (sizeof(C_update)/sizeof(botcmd_t)) - 1);
+  const botcmd_t *cmd = search_botcmd_t((const botcmd_t*)&C_update, code, lengthof(C_update) - 1);
   if (cmd) {
     /* Found a match */
     (cmd->func)(idx, msg);

+ 1 - 1
src/set.c

@@ -500,7 +500,7 @@ static inline variable_t *var_get_var_by_name(const char *name)
 {
   variable_t key;
   key.name = name;
-  return (variable_t*) bsearch(&key, &vars, (sizeof(vars)/sizeof(variable_t)) - 1, sizeof(variable_t), comp_variable_t);
+  return (variable_t*) bsearch(&key, &vars, lengthof(vars) - 1, sizeof(variable_t), comp_variable_t);
 }
 
 void var_set(variable_t *var, const char *target, const char *datain)