mirror of
https://github.com/mpv-player/mpv.git
synced 2025-12-28 05:33:14 +00:00
Audit and replace all ctype.h uses
Something like "char *s = ...; isdigit(s[0]);" triggers undefined behavior, because char can be signed, and thus s[0] can be a negative value. The is*() functions require unsigned char _or_ EOF. EOF is a special value outside of unsigned char range, thus the argument to the is*() functions can't be a char. This undefined behavior can actually trigger crashes if the implementation of these functions e.g. uses lookup tables, which are then indexed with out-of-range values. Replace all <ctype.h> uses with our own custom mp_is*() functions added with misc/ctype.h. As a bonus, these functions are locale-independent. (Although currently, we _require_ C locale for other reasons.)
This commit is contained in:
@@ -1,16 +1,16 @@
|
||||
#include <dirent.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <ctype.h>
|
||||
#include <assert.h>
|
||||
|
||||
#include "osdep/io.h"
|
||||
|
||||
#include "common/common.h"
|
||||
#include "common/global.h"
|
||||
#include "common/msg.h"
|
||||
#include "misc/ctype.h"
|
||||
#include "options/options.h"
|
||||
#include "options/path.h"
|
||||
#include "common/common.h"
|
||||
#include "sub/find_subfiles.h"
|
||||
|
||||
static const char *const sub_exts[] = {"utf", "utf8", "utf-8", "idx", "sub", "srt",
|
||||
@@ -75,7 +75,7 @@ static struct bstr guess_lang_from_filename(struct bstr name)
|
||||
|
||||
if (name.start[i] == ')' || name.start[i] == ']')
|
||||
i--;
|
||||
while (i >= 0 && isalpha(name.start[i])) {
|
||||
while (i >= 0 && mp_isalpha(name.start[i])) {
|
||||
n++;
|
||||
if (n > 3)
|
||||
return (struct bstr){NULL, 0};
|
||||
|
||||
@@ -24,7 +24,6 @@
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdbool.h>
|
||||
#include <ctype.h>
|
||||
#include <libavutil/common.h>
|
||||
|
||||
#include "common/msg.h"
|
||||
|
||||
12
sub/sd_srt.c
12
sub/sd_srt.c
@@ -24,11 +24,11 @@
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdbool.h>
|
||||
#include <ctype.h>
|
||||
#include <libavutil/common.h>
|
||||
|
||||
#include "common/common.h"
|
||||
#include "common/msg.h"
|
||||
#include "bstr/bstr.h"
|
||||
#include "misc/ctype.h"
|
||||
#include "sd.h"
|
||||
|
||||
struct line {
|
||||
@@ -259,7 +259,7 @@ static int read_attr(char **s, struct bstr *attr, struct bstr *val)
|
||||
attr->start = *s;
|
||||
attr->len = eq - *s;
|
||||
for (int i = 0; i < attr->len; i++)
|
||||
if (!isalnum(attr->start[i]))
|
||||
if (!mp_isalnum(attr->start[i]))
|
||||
return -1;
|
||||
val->start = eq + 1;
|
||||
bool quoted = val->start[0] == '"';
|
||||
@@ -290,7 +290,7 @@ static void convert_subrip(struct sd *sd, const char *orig,
|
||||
while (*line && new_line.len < new_line.bufsize - 1) {
|
||||
char *orig_line = line;
|
||||
|
||||
for (int i = 0; i < FF_ARRAY_ELEMS(subrip_basic_tags); i++) {
|
||||
for (int i = 0; i < MP_ARRAY_SIZE(subrip_basic_tags); i++) {
|
||||
const struct tag_conv *tag = &subrip_basic_tags[i];
|
||||
int from_len = strlen(tag->from);
|
||||
if (strncmp(line, tag->from, from_len) == 0) {
|
||||
@@ -331,7 +331,7 @@ static void convert_subrip(struct sd *sd, const char *orig,
|
||||
}
|
||||
}
|
||||
} else if (strncmp(line, "<font ", 6) == 0
|
||||
&& sp + 1 < FF_ARRAY_ELEMS(font_stack)) {
|
||||
&& sp + 1 < MP_ARRAY_SIZE(font_stack)) {
|
||||
/* Opening font tag */
|
||||
char *potential_font_tag_start = line;
|
||||
int len_backup = new_line.len;
|
||||
@@ -360,7 +360,7 @@ static void convert_subrip(struct sd *sd, const char *orig,
|
||||
int found = 0;
|
||||
|
||||
// Try to lookup the string in standard web colors
|
||||
for (int i = 0; i < FF_ARRAY_ELEMS(subrip_web_colors); i++) {
|
||||
for (int i = 0; i < MP_ARRAY_SIZE(subrip_web_colors); i++) {
|
||||
char *color = subrip_web_colors[i].s;
|
||||
if (bstrcasecmp(val, bstr0(color)) == 0) {
|
||||
uint32_t xcolor = subrip_web_colors[i].v;
|
||||
|
||||
Reference in New Issue
Block a user