[vlc-devel] commit: sprintf +n in file.c, plus add and use vlc_num_keys and vlc_num_modifiers. (JP Dinger )

git version control git at videolan.org
Wed May 20 11:12:20 CEST 2009


vlc | branch: master | JP Dinger <jpd at m2x.nl> | Tue May 19 10:03:22 2009 +0200| [133d58567b05e457fdab2643af45aa2478c86e30] | committer: JP Dinger 

sprintf +n in file.c, plus add and use vlc_num_keys and vlc_num_modifiers.

Signed-off-by: JP Dinger <jpd at videolan.org>

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=133d58567b05e457fdab2643af45aa2478c86e30
---

 include/vlc_keys.h                        |   11 +++++++----
 modules/gui/wince/preferences_widgets.cpp |    9 +++------
 src/config/file.c                         |   23 +++++++++++++----------
 3 files changed, 23 insertions(+), 20 deletions(-)

diff --git a/include/vlc_keys.h b/include/vlc_keys.h
index 450a6f3..f78fd3a 100644
--- a/include/vlc_keys.h
+++ b/include/vlc_keys.h
@@ -108,6 +108,8 @@ static const struct key_descriptor_s vlc_modifiers[] =
     { "Meta", KEY_MODIFIER_META },
     { "Command", KEY_MODIFIER_COMMAND }
 };
+enum { vlc_num_modifiers=sizeof(vlc_modifiers)
+                        /sizeof(struct key_descriptor_s) };
 
 static const struct key_descriptor_s vlc_keys[] =
 {
@@ -221,11 +223,12 @@ static const struct key_descriptor_s vlc_keys[] =
     { "Media Stop", KEY_MEDIA_STOP },
     { "Media Play Pause", KEY_MEDIA_PLAY_PAUSE }
 };
+enum { vlc_num_keys=sizeof(vlc_keys)/sizeof(struct key_descriptor_s) };
 
 static inline const char *KeyToString( int i_key )
 {
-    unsigned int i = 0;
-    for ( i = 0; i < sizeof(vlc_keys) / sizeof(key_descriptor_t); i++ )
+    size_t i;
+    for ( i = 0; i < vlc_num_keys; ++i )
     {
         if ( vlc_keys[i].i_key_code == i_key )
         {
@@ -237,8 +240,8 @@ static inline const char *KeyToString( int i_key )
 
 static inline int StringToKey( char *psz_key )
 {
-    unsigned int i = 0;
-    for ( i = 0; i < sizeof(vlc_keys) / sizeof(key_descriptor_t); i++ )
+    size_t i;
+    for ( i = 0; i < vlc_num_keys; ++i )
     {
         if ( !strcmp( vlc_keys[i].psz_key_string, psz_key ))
         {
diff --git a/modules/gui/wince/preferences_widgets.cpp b/modules/gui/wince/preferences_widgets.cpp
index 3bf2fc9..7fc13d4 100644
--- a/modules/gui/wince/preferences_widgets.cpp
+++ b/modules/gui/wince/preferences_widgets.cpp
@@ -171,14 +171,11 @@ KeyConfigControl::KeyConfigControl( vlc_object_t *p_this,
                                     int * py_pos )
   : ConfigControl( p_this, p_item, parent, hInst )
 {
-    // Number of keys descriptions
-    unsigned int i_keys = sizeof(vlc_keys)/sizeof(key_descriptor_t);
-
     // Init the keys decriptions array
     if( m_keysList == NULL )
     {
-        m_keysList = new string[i_keys];
-        for( unsigned int i = 0; i < i_keys; i++ )
+        m_keysList = new string[vlc_num_keys];
+        for( size_t i = 0; i < vlc_num_keys; ++i )
         {
             m_keysList[i] = vlc_keys[i].psz_key_string;
         }
@@ -233,7 +230,7 @@ KeyConfigControl::KeyConfigControl( vlc_object_t *p_this,
 
     *py_pos += 15 + 10;
 
-    for( unsigned int i = 0; i < i_keys ; i++ )
+    for( size_t i = 0; i < vlc_num_keys ; ++i )
     {
         ComboBox_AddString( combo, _FROMMB(m_keysList[i].c_str()) );
         ComboBox_SetItemData( combo, i, (void*)vlc_keys[i].i_key_code );
diff --git a/src/config/file.c b/src/config/file.c
index 6c411f2..9d12a97 100644
--- a/src/config/file.c
+++ b/src/config/file.c
@@ -744,11 +744,11 @@ int __config_SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name )
 int ConfigStringToKey( const char *psz_key )
 {
     int i_key = 0;
-    unsigned int i;
+    size_t i;
     const char *psz_parser = strchr( psz_key, '-' );
     while( psz_parser && psz_parser != psz_key )
     {
-        for( i = 0; i < sizeof(vlc_modifiers) / sizeof(key_descriptor_t); i++ )
+        for( i = 0; i < vlc_num_modifiers; ++i )
         {
             if( !strncasecmp( vlc_modifiers[i].psz_key_string, psz_key,
                               strlen( vlc_modifiers[i].psz_key_string ) ) )
@@ -759,7 +759,7 @@ int ConfigStringToKey( const char *psz_key )
         psz_key = psz_parser + 1;
         psz_parser = strchr( psz_key, '-' );
     }
-    for( i = 0; i < sizeof(vlc_keys) / sizeof( key_descriptor_t ); i++ )
+    for( i = 0; i < vlc_num_keys; ++i )
     {
         if( !strcasecmp( vlc_keys[i].psz_key_string, psz_key ) )
         {
@@ -772,7 +772,10 @@ int ConfigStringToKey( const char *psz_key )
 
 char *ConfigKeyToString( int i_key )
 {
-    char *psz_key = malloc( 100 );
+    // Worst case appears to be 45 characters:
+    // "Command-Meta-Ctrl-Shift-Alt-Browser Favorites"
+    enum { keylen=64 };
+    char *psz_key = malloc( keylen );
     char *p;
     size_t index;
 
@@ -783,20 +786,20 @@ char *ConfigKeyToString( int i_key )
     *psz_key = '\0';
     p = psz_key;
 
-    for( index = 0; index < (sizeof(vlc_modifiers) / sizeof(key_descriptor_t));
-         index++ )
+    for( index = 0; index < vlc_num_modifiers; ++index )
     {
         if( i_key & vlc_modifiers[index].i_key_code )
         {
-            p += sprintf( p, "%s-", vlc_modifiers[index].psz_key_string );
+            p += snprintf( p, keylen-(psz_key-p), "%s-",
+                           vlc_modifiers[index].psz_key_string );
         }
     }
-    for( index = 0; index < (sizeof(vlc_keys) / sizeof( key_descriptor_t));
-         index++)
+    for( index = 0; index < vlc_num_keys; ++index )
     {
         if( (int)( i_key & ~KEY_MODIFIER ) == vlc_keys[index].i_key_code )
         {
-            p += sprintf( p, "%s", vlc_keys[index].psz_key_string );
+            p += snprintf( p, keylen-(psz_key-p), "%s",
+                           vlc_keys[index].psz_key_string );
             break;
         }
     }




More information about the vlc-devel mailing list