mcstrans: fix off-by-one in MAX_CATS / maxbit comparisons
Stephen Smalley
maintainerPATCH 1/2 mcstrans: fix off-by-one in MAX_CATS / maxbit comparisons
For a given category name cN, N must be strictly less than MAX_CATS and maxbit, e.g. if MAX_CATS is 1024, the last category is c1023.
Signed-off-by Stephen Smalley <[email protected]>
--- mcstrans/src/mcstrans.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
mcstrans/src/mcstrans.c +4 -4
@@ -192,18 +192,18 @@ parse_category(ebitmap_t *e, const char *raw, bool allowinverse, }if (sscanf (raw,"c%u" , &low) !=1 )return -1 ; -if (low >MAX_CATS ) +if (low >=MAX_CATS )return -1 ; -if (!fromconfig && low > maxbit) +if (!fromconfig && low >= maxbit)return -1 ; raw +=numdigits (low) +1 ;if (*raw =='.' ) { raw++;if (sscanf (raw,"c%u" , &high) !=1 )return -1 ; -if (high >MAX_CATS ) +if (high >=MAX_CATS )return -1 ; -if (!fromconfig && high > maxbit) +if (!fromconfig && high >= maxbit)return -1 ; raw +=numdigits (high) +1 ; }else {
Signature · 1 line
2.54.0
Stephen Smalley
maintainerPATCH 2/2 mcstrans: cap the cache size
Cap the cache size and do not fail with an error if we are unable to cache a given translation.
Signed-off-by Stephen Smalley <[email protected]>
--- mcstrans/src/mcstrans.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-)
mcstrans/src/mcstrans.c +11 -1
@@ -38,6 +38,8 @@#include "mcstrans.h" #define N_BUCKETS 1453 +#define MAX_CACHE_ENTRIES 65536 +static unsigned int cache_entries;#define log_error (fmt, ...) fprintf(stderr, fmt, __VA_ARGS__)
@@ -648,7 +650,15 @@ add_base_classification(domain_t *domain, char *raw, char *trans) {static int add_cache (domain_t *domain,char *raw,char *trans) { -context_map_t *map =calloc (1 ,sizeof (context_map_t)); +context_map_t *map; + +if (cache_entries >MAX_CACHE_ENTRIES ) { +log_debug (" add_cache: cache full, not caching (%s, %s)\n" , + raw, trans); +return 0 ; + } + + map =calloc (1 ,sizeof (context_map_t));if (!map) goto err; map->raw =strdup (raw);
Signature · 1 line
2.54.0
Stephen Smalley
maintainerRe: [PATCH 2/2] mcstrans: cap the cache size
Quoted 8 lines · Stephen Smalley
Cap the cache size and do not fail with an error if we are unable to cache a given translation.
Signed-off-by Stephen Smalley <[email protected]>--- mcstrans/src/mcstrans.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-)
Sorry, disregard this patch - wasn't quite ready.
Quoted 34 lines
mcstrans/src/mcstrans.c +11
-2@@ -38,6 +38,8 @@#include "mcstrans.h" #define N_BUCKETS 1453 +#define MAX_CACHE_ENTRIES 65536 +static unsigned int cache_entries;#define log_error (fmt, ...) fprintf(stderr, fmt, __VA_ARGS__)@@ -648,7 +650,15 @@ add_base_classification(domain_t *domain, char *raw, char *trans) {static int add_cache (domain_t *domain,char *raw,char *trans) { -context_map_t *map =calloc (1 ,sizeof (context_map_t)); +context_map_t *map; + +if (cache_entries >MAX_CACHE_ENTRIES ) { +log_debug (" add_cache: cache full, not caching (%s, %s)\n" , + raw, trans); +return 0 ; + } + + map =calloc (1 ,sizeof (context_map_t));if (!map) goto err; map->raw =strdup (raw); --2.54.0
Stephen Smalley
maintainerQuoted 6 lines · Stephen Smalley
For a given category name cN, N must be strictly less than
MAX_CATSand maxbit, e.g. ifMAX_CATSis 1024, the last category is c1023.Signed-off-by Stephen Smalley <[email protected]>
Merged.
Quoted 34 lines
--- mcstrans/src/mcstrans.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)mcstrans/src/mcstrans.c +4
-5@@ -192,18 +192,18 @@ parse_category(ebitmap_t *e, const char *raw, bool allowinverse, }if (sscanf (raw,"c%u" , &low) !=1 )return -1 ; -if (low >MAX_CATS ) +if (low >=MAX_CATS )return -1 ; -if (!fromconfig && low > maxbit) +if (!fromconfig && low >= maxbit)return -1 ; raw +=numdigits (low) +1 ;if (*raw =='.' ) { raw++;if (sscanf (raw,"c%u" , &high) !=1 )return -1 ; -if (high >MAX_CATS ) +if (high >=MAX_CATS )return -1 ; -if (!fromconfig && high > maxbit) +if (!fromconfig && high >= maxbit)return -1 ; raw +=numdigits (high) +1 ; }else { --2.54.0