[hackers] [dwm][PATCH] applyrules: read rules[] with the `r` pointer directly
From: explosion-mental no need for `i` so remove the i variable. :gigachad: --- dwm.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/dwm.c b/dwm.c index e5efb6a..c2dcb15 100644 --- a/dwm.c +++ b/dwm.c @@ -280,7 +280,6 @@ void applyrules(Client *c) { const char *class, *instance; - unsigned int i; const Rule *r; Monitor *m; XClassHint ch = { NULL, NULL }; @@ -292,8 +291,7 @@ applyrules(Client *c) class= ch.res_class ? ch.res_class : broken; instance = ch.res_name ? ch.res_name : broken; - for (i = 0; i < LENGTH(rules); i++) { - r = &rules[i]; + for (r = &rules[0]; r <= &rules[LENGTH(rules) - 1]; r++) { if ((!r->title || strstr(c->name, r->title)) && (!r->class || strstr(class, r->class)) && (!r->instance || strstr(instance, r->instance))) -- 2.37.2
[hackers] [sbase] [PATCH] printf: Do not read past the end of the format string
If a trailing `%' character occurs, we read past the end of the format string and thus introduce UB. Reproducible by executing the following: ./printf % This happens because the format string here actually consists of two characters, `%' _and_ the trailing nul-byte. The flag parsing loop matches the nul-byte with `0' and thus increases the counter, `i', to be `formatlen + 1'. Furthermore, `i' is used as an index to access the format string, which will eventually lead to an out-of-bounds access. This can be fixed by simply checking the value of `i' like this: if (i > formatlen) eprintf(...); However, there are two more ways in which `i' can "overflow". The second "overflow" could happen after parsing the "field width". Given the following call, ./printf %42 we enter the loop with `i = 1'. The condition is matched _3_ times, again, because of the trailing nul-byte. `i' now has a value of `formatlen + 1'. This can be fixed by using the same check as above. The last way in which `i' might "overflow" is after parsing the "field precision". Take this call as an example: ./printf %.42 Here, we enter the loop with `i = formatlen'. After the dot (`.') is matched, `i' is increased by one and effectively "overflows". The fix for this is the same as above. I agree that it's a bit ugly to repeat this check three times, but the alternatives seem to add more cruft. Note that the last two cases only appear if numbers are used after the percent sign. Other characters don't match the conditions and just fall through to the `switch' statements default case, which emits an error. --- Although I've tested this, it's not unlikely that I've overseen something. As stated above, this solution isn't beautiful, but after playing around a bit with different approaches, it turned out to be the most convenient (I'm happy to be corrected on this). Anyways, it's definitely better than having UB in a tool which is marked as "finished". --- printf.c | 9 + 1 file changed, 9 insertions(+) diff --git a/printf.c b/printf.c index 039dac717105..75667dad8d36 100644 --- a/printf.c +++ b/printf.c @@ -54,6 +54,9 @@ main(int argc, char *argv[]) flag = format[i]; } + if (i > formatlen) + eprintf("Missing conversion specifier.\n"); + /* field width */ width = -1; if (format[i] == '*') { @@ -74,6 +77,9 @@ main(int argc, char *argv[]) } } + if (i > formatlen) + eprintf("Missing conversion specifier.\n"); + /* field precision */ precision = -1; if (format[i] == '.') { @@ -96,6 +102,9 @@ main(int argc, char *argv[]) } } + if (i > formatlen) + eprintf("Missing conversion specifier.\n"); + if (format[i] != '%') { if (argi < argc) arg = argv[argi++]; -- 2.37.2
[hackers] [surf][PATCH 2/2] surf-open.sh: Remove spurious white space
--- surf-open.sh | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/surf-open.sh b/surf-open.sh index 4dfce4a..446b3a4 100755 --- a/surf-open.sh +++ b/surf-open.sh @@ -1,6 +1,6 @@ #!/bin/sh # -# See the LICENSE file for copyright and license details. +# See the LICENSE file for copyright and license details. # xidfile="${TMPDIR=/tmp}/tabbed-surf-$USER.xid" @@ -31,4 +31,3 @@ else surf -e "$xid" "$uri" >/dev/null 2>&1 & fi fi - -- 2.33.3
[hackers] [surf][PATCH 1/2] surf-open.sh: Use TMPDIR if set or /tmp instead of ~/tmp
Either is guaranteed to exist by POSIX, as opposed to ~/tmp. --- surf-open.sh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/surf-open.sh b/surf-open.sh index c22edc2..4dfce4a 100755 --- a/surf-open.sh +++ b/surf-open.sh @@ -3,7 +3,7 @@ # See the LICENSE file for copyright and license details. # -xidfile="$HOME/tmp/tabbed-surf.xid" +xidfile="${TMPDIR=/tmp}/tabbed-surf-$USER.xid" uri="" if [ "$#" -gt 0 ]; @@ -12,6 +12,8 @@ then fi runtabbed() { + touch "$xidfile" + chmod 600 "$xidfile" tabbed -dn tabbed-surf -r 2 surf -e '' "$uri" >"$xidfile" \ 2>/dev/null & } -- 2.33.3
Re: [hackers] [libgrapheme] Add manuals for the grapheme_to_*case_utf8-functions || Laslo Hunhold
On Sun, 28 Aug 2022 20:00:42 +0200 Quentin Rameau wrote: Dear Quentin, > But of course, that's why the construction ${variable} exists, > for this very common case. > It's clear that UNITs isn't a variable, > so you need to separate the variable from the string. > You don't that with a subshell and printf, > you do that with just ${UNIT}s. thanks for your explanation and pointing this out! I totally forgot about this and have now pushed a change to use the proper parameter expansion[0]. For those interested, here's the excerpt from the POSIX-standard[1]. Also thanks to you, Thomas Oltmann, for pointing this out as well. With best regards Laslo [0]:https://git.suckless.org/libgrapheme/commit/6e6c538e4efb4d191a2f0391466556eb758d76bd.html [1]:https://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_06_02
[hackers] [libgrapheme] Use parameter expansion of variables within heredoc || Laslo Hunhold
commit 6e6c538e4efb4d191a2f0391466556eb758d76bd Author: Laslo Hunhold AuthorDate: Mon Aug 29 10:31:54 2022 +0200 Commit: Laslo Hunhold CommitDate: Mon Aug 29 10:31:54 2022 +0200 Use parameter expansion of variables within heredoc Thanks to Quentin Rameau and Thomas Oltmann for remarking that the workaround $(printf $VARIABLE) was not one of my brightest ideas, given you can just use ${VARIABLE} instead. Additionally, make use of an $ANTISUFFIX variable in the next_break-template. Signed-off-by: Laslo Hunhold diff --git a/man/grapheme_decode_utf8.sh b/man/grapheme_decode_utf8.sh index 584025f..f2321d0 100644 --- a/man/grapheme_decode_utf8.sh +++ b/man/grapheme_decode_utf8.sh @@ -1,5 +1,5 @@ cat << EOF -.Dd $MAN_DATE +.Dd ${MAN_DATE} .Dt GRAPHEME_DECODE_UTF8 3 .Os suckless.org .Sh NAME diff --git a/man/grapheme_encode_utf8.sh b/man/grapheme_encode_utf8.sh index d1ecad7..6df8478 100644 --- a/man/grapheme_encode_utf8.sh +++ b/man/grapheme_encode_utf8.sh @@ -1,5 +1,5 @@ cat << EOF -.Dd $MAN_DATE +.Dd ${MAN_DATE} .Dt GRAPHEME_ENCODE_UTF8 3 .Os suckless.org .Sh NAME diff --git a/man/grapheme_is_character_break.sh b/man/grapheme_is_character_break.sh index 86235ce..995fd7a 100644 --- a/man/grapheme_is_character_break.sh +++ b/man/grapheme_is_character_break.sh @@ -1,5 +1,5 @@ cat << EOF -.Dd $MAN_DATE +.Dd ${MAN_DATE} .Dt GRAPHEME_IS_CHARACTER_BREAK 3 .Os suckless.org .Sh NAME @@ -77,7 +77,7 @@ main(void) .Xr libgrapheme 7 .Sh STANDARDS .Fn grapheme_is_character_break -is compliant with the Unicode 14.0.0 specification. +is compliant with the Unicode ${UNICODE_VERSION} specification. .Sh AUTHORS .An Laslo Hunhold Aq Mt d...@frign.de EOF diff --git a/man/libgrapheme.sh b/man/libgrapheme.sh index 47bbe34..ae1f258 100644 --- a/man/libgrapheme.sh +++ b/man/libgrapheme.sh @@ -1,5 +1,5 @@ cat << EOF -.Dd $MAN_DATE +.Dd ${MAN_DATE} .Dt LIBGRAPHEME 7 .Os suckless.org .Sh NAME @@ -55,7 +55,7 @@ example illustrating the possible usage. .Xr grapheme_to_titlecase_utf8 3 .Sh STANDARDS .Nm -is compliant with the Unicode $UNICODE_VERSION specification. +is compliant with the Unicode ${UNICODE_VERSION} specification. .Sh MOTIVATION The idea behind every character encoding scheme like ASCII or Unicode is to express abstract characters (which can be thought of as shapes diff --git a/man/template/next_break.sh b/man/template/next_break.sh index e268ad1..c25ab38 100644 --- a/man/template/next_break.sh +++ b/man/template/next_break.sh @@ -1,34 +1,36 @@ if [ "$ENCODING" = "utf8" ]; then UNIT="byte" SUFFIX="_utf8" + ANTISUFFIX="" else UNIT="codepoint" SUFFIX="" + ANTISUFFIX="_utf8" fi cat << EOF -.Dd $MAN_DATE +.Dd ${MAN_DATE} .Dt GRAPHEME_NEXT_$(printf "%s_break%s" "$TYPE" "$SUFFIX" | tr [:lower:] [:upper:]) 3 .Os suckless.org .Sh NAME -.Nm grapheme_next_$(printf $TYPE)_break$SUFFIX -.Nd determine $UNIT-offset to next $REALTYPE break +.Nm grapheme_next_${TYPE}_break${SUFFIX} +.Nd determine ${UNIT}-offset to next ${REALTYPE} break .Sh SYNOPSIS .In grapheme.h .Ft size_t -.Fn grapheme_next_$(printf $TYPE)_break$SUFFIX "const $(if [ "$ENCODING" = "utf8" ]; then printf "char"; else printf "uint_least32_t"; fi) *str" "size_t len" +.Fn grapheme_next_${TYPE}_break${SUFFIX} "const $(if [ "$ENCODING" = "utf8" ]; then printf "char"; else printf "uint_least32_t"; fi) *str" "size_t len" .Sh DESCRIPTION The -.Fn grapheme_next_$(printf $TYPE)_break$SUFFIX -function computes the offset (in $(printf $UNIT)s) to the next $REALTYPE +.Fn grapheme_next_${TYPE}_break${SUFFIX} +function computes the offset (in ${UNIT}s) to the next ${REALTYPE} break (see .Xr libgrapheme 7 ) in the $(if [ "$ENCODING" = "utf8" ]; then printf "UTF-8-encoded string"; else printf "codepoint array"; fi) .Va str of length -.Va len .$(if [ "$TYPE" != "line" ]; then printf "\nIf a $REALTYPE begins at +.Va len .$(if [ "$TYPE" != "line" ]; then printf "\nIf a ${REALTYPE} begins at .Va str -this offset is equal to the length of said $REALTYPE."; fi) +this offset is equal to the length of said ${REALTYPE}."; fi) .Pp If .Va len @@ -41,12 +43,12 @@ NUL-byte is encountered. .Pp For $(if [ "$ENCODING" != "utf8" ]; then printf "UTF-8-encoded"; else printf "non-UTF-8"; fi) input data$(if [ "$TYPE" = "character" ] && [ "$ENCODING" = "utf8" ]; then printf "\n.Xr grapheme_is_character_break 3 and"; fi) -.Xr grapheme_next_$(printf $TYPE)_break$(if [ "$ENCODING" != "utf8" ]; then printf "_utf8"; fi) 3 +.Xr grapheme_next_${TYPE}_break${ANTISUFFIX} can be used instead. .Sh RETURN VALUES The -.Fn grapheme_next_$(printf $TYPE)_break$SUFFIX -function returns the offset (in $(printf $UNIT)s) to the next $REALTYPE +.Fn grapheme_next_${TYPE}_break${SUFFIX} +function returns the offset (in ${UNIT}s) to the next ${REALTYPE} break in .Va str or 0 if @@ -76,19 +78,19 @@ main(void) printf("Input: "%s"n", s); - /* pr
[hackers] [libgrapheme] Quote input variables || Laslo Hunhold
commit c58eb2ceb965785dbe9fecb688e10250d17aeca7 Author: Laslo Hunhold AuthorDate: Mon Aug 29 10:30:00 2022 +0200 Commit: Laslo Hunhold CommitDate: Mon Aug 29 10:30:00 2022 +0200 Quote input variables The passed values can be expected to never contain spaces, but it's better to be safe. Just theoretically, nobody is prevented from using a sh-binary located in "/opt/my favourite bins/sh". Signed-off-by: Laslo Hunhold diff --git a/Makefile b/Makefile index bd1ca96..ac0e5f1 100644 --- a/Makefile +++ b/Makefile @@ -243,10 +243,10 @@ libgrapheme.so: $(SRC:=.o) $(CC) -o $@ $(SOFLAGS) $(LDFLAGS) $(SRC:=.o) $(MAN3:=.3): - SH=$(SH) MAN_DATE=$(MAN_DATE) UNICODE_VERSION=$(UNICODE_VERSION) $(SH) $(@:.3=.sh) > $@ + SH="$(SH)" MAN_DATE="$(MAN_DATE)" UNICODE_VERSION="$(UNICODE_VERSION)" $(SH) $(@:.3=.sh) > $@ $(MAN7:=.7): - SH=$(SH) MAN_DATE=$(MAN_DATE) UNICODE_VERSION=$(UNICODE_VERSION) $(SH) $(@:.7=.sh) > $@ + SH="$(SH)" MAN_DATE="$(MAN_DATE)" UNICODE_VERSION="$(UNICODE_VERSION)" $(SH) $(@:.7=.sh) > $@ benchmark: $(BENCHMARK) for m in $(BENCHMARK); do ./$$m; done