5.1.21 · D3 · HinglishC Programming

Worked examplesSafe alternatives — strncpy, snprintf, strlcpy

2,718 words12 min read↑ Read in English

5.1.21 · D3 · Coding › C Programming › Safe alternatives — strncpy, snprintf, strlcpy

Figure — Safe alternatives — strncpy, snprintf, strlcpy

Scenario matrix

Har woh case jo ek copy function ko hit kar sakta hai, inhi cells mein se ek hai. Har column source text aur destination capacity ke beech ek length relationship hai; har degenerate row ek edge input hai.

Cell Situation Kya hona chahiye Kaunsa example
A src, se chota hai fully copies + terminate karta hai; spare room Ex 1
B src ki length exactly bilkul fit hota hai, terminate karta hai, koi truncation nahi Ex 2
C src ki length exactly the trapstrncpy knot drop kar deta hai Ex 3
D src, se bahut lamba truncation, usse detect karo Ex 4
E src empty ("") sirf '\0' likhta hai Ex 5
F size = 0 pass kiya function ko kuch bhi touch nahi karna chahiye Ex 6
G real-world: ek path banao, overflow detect karo snprintf + return check Ex 7
H exam twist: chained copies, off-by-one knot ke baare mein reason karo Ex 8

Hum char buf[8] () almost har jagah use karte hain, isliye index last box hai aur hamara content cap hai.


Ex 1 — Cell A: short source, room to spare


Ex 2 — Cell B: source length exactly


Ex 3 — Cell C: source length exactly (THE TRAP)

Figure — Safe alternatives — strncpy, snprintf, strlcpy

Ex 4 — Cell D: source bahut lamba, teeno functions compare karo


Ex 5 — Cell E: empty source


Ex 6 — Cell F: size = 0 edge


Ex 7 — Cell G: real-world path join with overflow detection


Ex 8 — Cell H: exam twist, chained copies aur off-by-one


Cell coverage check

Ex 1

Ex 2

Ex 3

Ex 4

Ex 5

Ex 6

Ex 7

Ex 8

scenario matrix

A short src

B exact C-1

C exact C trap

D oversized

E empty src

F size zero

G real path

H exam chain

all cells covered

Recall Har cell ka ek-line summary

Short src safely pad karta hai; exact-C-1 fit hota hai aur terminate karta hai; exact-C trap hai (strncpy knot drop kar deta hai); oversized truncate karta hai (r >= size se detect karo); empty sirf ek knot likhta hai; size=0 kuch touch nahi karta; real code return ko capacity se compare karta hai; aur ek short source re-copy zero-fill karke purana garbage saaf kar deta hai.

Kaun sa cell hai jo silently ek unterminated string produce karta hai?
Cell C — source length exactly capacity ke equal hai, toh strncpy saare bytes copy karta hai aur koi '\0' add nahi karta.
Ex 7 mein, guard r >= sizeof(path) fire kyun nahi karta?
Intended text 15 chars hai jise 16 bytes chahiye, capacity 16 hai, toh yeh exactly fit hota hai; r == 15 aur 15 >= 16 false hai.
Ex 8 mein, line 3 bina manual fix ke termination kyun restore kar deta hai?
"ab" n se chota hai, toh strncpy boxes 2–4 ko '\0' se pad karta hai, pehle ka content clean up karke automatically terminate kar deta hai.

Connections

  • Parent · Safe alternatives
  • C strings and the null terminator
  • Buffer overflow and stack smashing
  • strcpy and sprintf (unsafe)
  • sizeof vs strlen
  • Format strings and printf family
  • Memory safety and undefined behavior