Changeset 37e4025 in mainline


Ignore:
Timestamp:
2011-11-04T22:22:05Z (13 years ago)
Author:
Jan Vesely <jano.vesely@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
99a1a56
Parents:
2179cf95
Message:

libusbdev: Use asprintf instead of 256 byte buffer for match strings.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/usbdev/src/recognise.c

    r2179cf95 r37e4025  
    6666#define BCD_ARGS(a) BCD_INT((a)), BCD_FRAC((a))
    6767
    68 /* FIXME: make this dynamic */
    69 #define MATCH_STRING_MAX 256
    70 
    7168/** Add formatted match id.
    7269 *
     
    7774 */
    7875static int usb_add_match_id(match_id_list_t *matches, int score,
    79     const char *format, ...)
     76    const char *match_str)
    8077{
    81         char *match_str = NULL;
    82         match_id_t *match_id = NULL;
    83         int rc;
    84        
    85         match_str = malloc(MATCH_STRING_MAX + 1);
    86         if (match_str == NULL) {
    87                 rc = ENOMEM;
    88                 goto failure;
    89         }
    90 
    91         /*
    92          * FIXME: replace with dynamic allocation of exact size
    93          */
    94         va_list args;
    95         va_start(args, format   );
    96         vsnprintf(match_str, MATCH_STRING_MAX, format, args);
    97         match_str[MATCH_STRING_MAX] = 0;
    98         va_end(args);
    99 
    100         match_id = create_match_id();
     78        assert(matches);
     79
     80        match_id_t *match_id = create_match_id();
    10181        if (match_id == NULL) {
    102                 rc = ENOMEM;
    103                 goto failure;
     82                return ENOMEM;
    10483        }
    10584
     
    10988
    11089        return EOK;
    111        
    112 failure:
    113         if (match_str != NULL) {
    114                 free(match_str);
    115         }
    116         if (match_id != NULL) {
    117                 match_id->id = NULL;
    118                 delete_match_id(match_id);
    119         }
    120        
    121         return rc;
    12290}
    12391
     
    13199#define ADD_MATCHID_OR_RETURN(match_ids, score, format, ...) \
    132100        do { \
    133                 int __rc = usb_add_match_id((match_ids), (score), \
    134                     format, ##__VA_ARGS__); \
     101                char *str = NULL; \
     102                int __rc = asprintf(&str, format, ##__VA_ARGS__); \
     103                if (__rc > 0) { \
     104                        __rc = usb_add_match_id((match_ids), (score), str); \
     105                } \
    135106                if (__rc != EOK) { \
     107                        free(str); \
    136108                        return __rc; \
    137109                } \
     
    152124    match_id_list_t *matches)
    153125{
    154         if (desc_interface == NULL) {
    155                 return EINVAL;
    156         }
    157         if (matches == NULL) {
     126        if (desc_interface == NULL || matches == NULL) {
    158127                return EINVAL;
    159128        }
Note: See TracChangeset for help on using the changeset viewer.