Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit5fa870a

Browse files
committed
cache: support string without a NUL terminator
Also we, by which I mean me, might have been using HASH_ADD_STR wrong...Signed-off-by: Yuxuan Shui <yshuiv7@gmail.com>
1 parent664f8b8 commit5fa870a

File tree

6 files changed

+33
-24
lines changed

6 files changed

+33
-24
lines changed

‎src/atom.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ struct atom_entry {
1414
};
1515

1616
staticinlineintatom_getter(structcache*cacheattr_unused,constchar*atom_name,
17-
structcache_handle**value,void*user_data) {
17+
size_tkeylen,structcache_handle**value,void*user_data) {
1818
xcb_connection_t*c=user_data;
1919
xcb_intern_atom_reply_t*reply=xcb_intern_atom_reply(
20-
c,xcb_intern_atom(c,0,to_u16_checked(strlen(atom_name)),atom_name),NULL);
20+
c,xcb_intern_atom(c,0,to_u16_checked(keylen),atom_name),NULL);
2121

2222
xcb_atom_tatom=XCB_NONE;
2323
if (reply) {
24-
log_debug("Atom %s is %d",atom_name,reply->atom);
24+
log_debug("Atom %.*s is %d", (int)keylen,atom_name,reply->atom);
2525
atom=reply->atom;
2626
free(reply);
2727
}else {
@@ -41,17 +41,17 @@ atom_entry_free(struct cache *cache attr_unused, struct cache_handle *handle) {
4141
free(entry);
4242
}
4343

44-
xcb_atom_tget_atom(structatom*a,constchar*key,xcb_connection_t*c) {
44+
xcb_atom_tget_atom(structatom*a,constchar*key,size_tkeylen,xcb_connection_t*c) {
4545
structcache_handle*entry=NULL;
46-
if (cache_get_or_fetch(&a->c,key,&entry,c,atom_getter)<0) {
46+
if (cache_get_or_fetch(&a->c,key,keylen,&entry,c,atom_getter)<0) {
4747
log_error("Failed to get atom %s",key);
4848
returnXCB_NONE;
4949
}
5050
returncache_entry(entry,structatom_entry,entry)->atom;
5151
}
5252

53-
xcb_atom_tget_atom_cached(structatom*a,constchar*key) {
54-
returncache_entry(cache_get(&a->c,key),structatom_entry,entry)->atom;
53+
xcb_atom_tget_atom_cached(structatom*a,constchar*key,size_tkeylen) {
54+
returncache_entry(cache_get(&a->c,key,keylen),structatom_entry,entry)->atom;
5555
}
5656

5757
/**
@@ -60,7 +60,7 @@ xcb_atom_t get_atom_cached(struct atom *a, const char *key) {
6060
structatom*init_atoms(xcb_connection_t*c) {
6161
autoatoms=ccalloc(1,structatom);
6262
atoms->c=CACHE_INIT;
63-
#defineATOM_GET(x) atoms->a##x = get_atom(atoms, #x, c)
63+
#defineATOM_GET(x) atoms->a##x = get_atom(atoms, #x,sizeof(#x) - 1,c)
6464
LIST_APPLY(ATOM_GET,SEP_COLON,ATOM_LIST1);
6565
LIST_APPLY(ATOM_GET,SEP_COLON,ATOM_LIST2);
6666
#undef ATOM_GET

‎src/atom.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,14 @@ struct atom {
6262
/// a reference to the connection.
6363
structatom*init_atoms(xcb_connection_t*c);
6464

65-
xcb_atom_tget_atom(structatom*a,constchar*key,xcb_connection_t*c);
66-
xcb_atom_tget_atom_cached(structatom*a,constchar*key);
65+
xcb_atom_tget_atom(structatom*a,constchar*key,size_tkeylen,xcb_connection_t*c);
66+
staticinlinexcb_atom_t
67+
get_atom_with_nul(structatom*a,constchar*key,xcb_connection_t*c) {
68+
returnget_atom(a,key,strlen(key),c);
69+
}
70+
xcb_atom_tget_atom_cached(structatom*a,constchar*key,size_tkeylen);
71+
staticinlinexcb_atom_tget_atom_cached_with_nul(structatom*a,constchar*key) {
72+
returnget_atom_cached(a,key,strlen(key));
73+
}
6774

6875
voiddestroy_atoms(structatom*a);

‎src/c2.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1151,7 +1151,7 @@ static bool c2_l_postprocess(struct c2_state *state, xcb_connection_t *c, c2_l_t
11511151

11521152
// Get target atom if it's not a predefined one
11531153
if (pleaf->predef==C2_L_PUNDEFINED) {
1154-
pleaf->tgtatom=get_atom(state->atoms,pleaf->tgt,c);
1154+
pleaf->tgtatom=get_atom_with_nul(state->atoms,pleaf->tgt,c);
11551155
if (!pleaf->tgtatom) {
11561156
log_error("Failed to get atom for target \"%s\".",pleaf->tgt);
11571157
return false;

‎src/cache.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,29 @@
22

33
#include"cache.h"
44

5-
structcache_handle*cache_get(structcache*c,constchar*key) {
5+
structcache_handle*cache_get(structcache*c,constchar*key,size_tkeylen) {
66
structcache_handle*e;
7-
HASH_FIND_STR(c->entries,key,e);
7+
HASH_FIND(hh,c->entries,key,keylen,e);
88
returne;
99
}
1010

11-
intcache_get_or_fetch(structcache*c,constchar*key,structcache_handle**value,
12-
void*user_data,cache_getter_tgetter) {
13-
*value=cache_get(c,key);
11+
intcache_get_or_fetch(structcache*c,constchar*key,size_tkeylen,
12+
structcache_handle**value,void*user_data,cache_getter_tgetter) {
13+
*value=cache_get(c,key,keylen);
1414
if (*value) {
1515
return0;
1616
}
1717

18-
interr=getter(c,key,value,user_data);
18+
interr=getter(c,key,keylen,value,user_data);
1919
assert(err <=0);
2020
if (err<0) {
2121
returnerr;
2222
}
23-
(*value)->key=strdup(key);
23+
// Add a NUL terminator to make things easier
24+
(*value)->key=cvalloc(keylen+1);
25+
memcpy((*value)->key,key,keylen);
2426

25-
HASH_ADD_STR(c->entries,key,*value);
27+
HASH_ADD_KEYPTR(hh,c->entries,(*value)->key,keylen,*value);
2628
return1;
2729
}
2830

‎src/cache.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ struct cache_handle;
1212
/// Should return 0 if the value is fetched successfully, and a negative number if the
1313
/// value cannot be fetched. Getter doesn't need to initialize fields of `struct
1414
/// cache_handle`.
15-
typedefint (*cache_getter_t)(structcache*,constchar*key,
15+
typedefint (*cache_getter_t)(structcache*,constchar*key,size_tkeylen,
1616
structcache_handle**value,void*user_data);
1717
typedefvoid (*cache_free_t)(structcache*,structcache_handle*value);
1818

@@ -31,12 +31,12 @@ struct cache_handle {
3131
/// getter will be called, and the returned value will be stored into the cache.
3232
/// Returns 0 if the value is already present in the cache, 1 if the value is fetched
3333
/// successfully, and a negative number if the value cannot be fetched.
34-
intcache_get_or_fetch(structcache*,constchar*key,structcache_handle**value,
35-
void*user_data,cache_getter_tgetter);
34+
intcache_get_or_fetch(structcache*,constchar*key,size_tkeylen,
35+
structcache_handle**value,void*user_data,cache_getter_tgetter);
3636

3737
/// Get a value from the cache. If the value doesn't present in the cache, NULL will be
3838
/// returned.
39-
structcache_handle*cache_get(structcache*,constchar*key);
39+
structcache_handle*cache_get(structcache*,constchar*key,size_tkeylen);
4040

4141
/// Invalidate all values in the cache. After this call, `struct cache` holds no allocated
4242
/// memory, and can be discarded.

‎src/picom.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1345,7 +1345,7 @@ static int register_cm(session_t *ps) {
13451345
log_fatal("Failed to allocate memory");
13461346
return-1;
13471347
}
1348-
atom=get_atom(ps->atoms,buf,ps->c.c);
1348+
atom=get_atom_with_nul(ps->atoms,buf,ps->c.c);
13491349
free(buf);
13501350

13511351
xcb_get_selection_owner_reply_t*reply=xcb_get_selection_owner_reply(

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp