5.1.21 · HinglishC Programming

Safe alternatives — strncpy, snprintf, strlcpy

1,676 words8 min readRead in English

5.1.21 · Coding › C Programming


YE sab kyun chahiye?

char buf[8];
strcpy(buf, "hello world");   // 12 bytes into 8 → OVERFLOW

Fix: buf ka size pass karo taaki copy waqt par ruk sake. Teen tools yeh karte hain, aur har ek mein ek crucial detail alag hoti hai.


strncpy — bounded copy with a trap


snprintf — formatting jo overflow nahi kar sakta


strlcpy — woh jo sab theek karta hai (BSD)

Figure — Safe alternatives — strncpy, snprintf, strlcpy

Comparison table (yeh memorize karo, baaki skip)

Function Cap karta hai Hamesha null-terminate? Return value Truncation check
strcpy kuch nahi haan (agar overflow nahi) dest ptr ❌ unsafe
strncpy n bytes NAHI dest ptr manual
snprintf size (\0 including) HAAN would-be length r >= size
strlcpy size-1 HAAN strlen(src) ret >= size
Recall Feynman: ek 12-saal ke bachche ko samjhao

Socho paani ek cup mein daal rahe ho. strcpy tab bhi daalti rehti hai jab cup bhar jaata hai — paani sab jagah gir jaata hai aur tumhara desk kharab ho jaata hai (woh crash ya hack hai). "Safe" daalney wale pehle poochte hain "cup kitna bada hai?" strncpy daalana rok deta hai lekin kabhi kabhi dhakkan lagana bhool jaata hai (koi '\0' nahi). snprintf hamesha dhakkan ke liye jagah chodta hai aur bolta hai "tumhe jo chahiye tha woh fit nahi hua." strlcpy sabse polite hai: waqt par ruk jaata hai, hamesha dhakkan lagata hai, aur dhire se batata hai ki tumhare paas actually kitna tha. Size argument woh sawaal hai "cup kitna bada hai?" — kabhi skip mat karo.


Flashcards

Safe string functions ko safe banane wala ek extra argument kya hai?
Destination buffer ka size, taaki copy overflow hone se pehle ruk sake.
strncpy null-terminate kab nahi karta?
Jab source length >= n (size limit) ho: exactly n bytes copy karta hai aur koi '\0' add nahi karta.
strncpy(dest, src, sizeof(dest)-1) ke baad standard fix kya hai?
Manually dest[sizeof(dest)-1] = '\0'; set karo termination guarantee karne ke liye.
sizeof(dest) ki jagah zyada se zyada sizeof(dest)-1 kyun copy karte hain?
Trailing '\0' ke liye ek byte reserved rehni chahiye; capacity − 1 max content hai.
Kya snprintf hamesha null-terminate karta hai?
Haan, jab tak size > 0 ho, yeh hamesha terminating '\0' likhta hai.
snprintf kya return karta hai aur truncation kaise detect karte hain?
Woh chars ki count return karta hai jo yeh likhta (excluding '\0'); truncation iff return >= size.
strlcpy aur strncpy mein kya fark hai?
strlcpy hamesha null-terminate karta hai aur zero-pad nahi karta; size-1 par cap karta hai aur strlen(src) return karta hai.
strlcpy hamesha usable kyun nahi hai?
Yeh non-standard hai (BSD origin); sab platforms par nahi — glibc ne ise sirf 2.38 mein add kiya.
char b[8]; snprintf(b,8,"id=%d",12345);b mein kya hai aur yeh kya return karta hai?
b = "id=1234" (7 chars + null); 8 return karta hai, truncation flag karta hai kyunki 8 >= 8.
Yeh functions kaunsa memory bug prevent karte hain?
Buffer overflow — destination array ke end ke baad likhna.

Connections

  • 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

Concept Map

no size check causes

prevents

takes

takes

takes

when src short

trap when src fills

requires

uses

reserves slot for null

Buffer overflow

strcpy and sprintf

Pass destination size n

strncpy dest src n

snprintf

strlcpy

Pads with nulls if src short

No null if src fills buffer

Force terminate manually

Cap at C minus 1