History

lavu/opt: parse key into a mallocated buffer.

It removes the hardcoded limit on the key size without making the code much more complex, and it makes for a more versatile API.

Nicolas George committed 14 years ago

Commit e021eeb1 parent 4e6fa5b1 file · +17 −18Browse files

Unified · Split

libavutil/opt.c+17−18
@@ -829,31 +829,27 @@ static int is_key_char(char c) · 828 unmodified lines
829829 * Read a key from a string.
830830 *
831831 * The key consists of is_key_char characters and must be terminated by a
832− * character from the delim string; spaces are ignored. The key buffer must
833− * be 4 bytes larger than the longest acceptable key. If the key is too
834− * long, an ellipsis will be written at the end.
832+ * character from the delim string; spaces are ignored.
835833 *
836834 * @return 0 for success (even with ellipsis), <0 for failure
837835 */
838−static int get_key(const char **ropts, const char *delim, char *key, unsigned key_size)
836+static int get_key(const char **ropts, const char *delim, char **rkey)
839837 {
840− unsigned key_pos = 0;
841838 const char *opts = *ropts;
839+ const char *key_start, *key_end;
842840
843− opts += strspn(opts, WHITESPACES);
844− while (is_key_char(*opts)) {
845− key[key_pos++] = *opts;
846− if (key_pos == key_size)
847− key_pos--;
848− (opts)++;
849− }
841+ key_start = opts += strspn(opts, WHITESPACES);
842+ while (is_key_char(*opts))
843+ opts++;
844+ key_end = opts;
850845 opts += strspn(opts, WHITESPACES);
851846 if (!*opts || !strchr(delim, *opts))
852847 return AVERROR(EINVAL);
853848 opts++;
854− key[key_pos++] = 0;
855− if (key_pos == key_size)
856− key[key_pos - 4] = key[key_pos - 3] = key[key_pos - 2] = '.';
849+ if (!(*rkey = av_malloc(key_end - key_start + 1)))
850+ return AVERROR(ENOMEM);
851+ memcpy(*rkey, key_start, key_end - key_start);
852+ (*rkey)[key_end - key_start] = 0;
857853 *ropts = opts;
858854 return 0;
859855 }
@@ -864,7 +860,7 @@ int av_opt_set_from_string(void *ctx, const char *opts, · 4 unmodified lines
864860 {
865861 int ret, count = 0;
866862 const char *dummy_shorthand = NULL;
867− char key_buf[68], *value;
863+ char *parsed_key, *value;
868864 const char *key;
869865
870866 if (!opts)
@@ -873,7 +869,8 @@ int av_opt_set_from_string(void *ctx, const char *opts, · 2 unmodified lines
873869 shorthand = &dummy_shorthand;
874870
875871 while (*opts) {
876− if ((ret = get_key(&opts, key_val_sep, key_buf, sizeof(key_buf))) < 0) {
872+ parsed_key = NULL; /* so we can free it anyway */
873+ if ((ret = get_key(&opts, key_val_sep, &parsed_key)) < 0) {
877874 if (*shorthand) {
878875 key = *(shorthand++);
879876 } else {
@@ -881,7 +878,7 @@ int av_opt_set_from_string(void *ctx, const char *opts, · 1 unmodified line
881878 return AVERROR(EINVAL);
882879 }
883880 } else {
884− key = key_buf;
881+ key = parsed_key;
885882 while (*shorthand) /* discard all remaining shorthand */
886883 shorthand++;
887884 }
@@ -896,10 +893,12 @@ int av_opt_set_from_string(void *ctx, const char *opts, · 8 unmodified lines
896893 if (ret == AVERROR_OPTION_NOT_FOUND)
897894 av_log(ctx, AV_LOG_ERROR, "Option '%s' not found\n", key);
898895 av_free(value);
896+ av_free(parsed_key);
899897 return ret;
900898 }
901899
902900 av_free(value);
901+ av_free(parsed_key);
903902 count++;
904903 }
905904 return count;

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t